Web Workers and the Main Thread
Move the work that blocks input off the thread that handles it, and know what it costs to send data across.
In this chapter: what actually blocks · the worker model · structured clone and transferables · Comlink · when a worker is the wrong answer · the INP connection
💡 The Core Idea
The main thread does three jobs: run your JavaScript, calculate layout and paint, and respond to input. It does them one at a time. A function that runs for 300 milliseconds blocks everything for 300 milliseconds. A tap does nothing, an animation holds a frame, and the page looks broken. It is broken.
A worker is a second thread with its own JavaScript engine and no access to the DOM. You send it a message, it does the work, and it sends a result back. The main thread stays free for the two jobs only it can do: rendering, and responding to the user.
Workers do not make anything faster. They make the expensive thing happen somewhere that nobody is waiting on.
How It Works
A worker is a separate script with a separate global scope. Nothing is shared except what you send.
Starting one and talking to it:
// The module worker form — `type: "module"` allows `import` inside the worker.
const worker = new Worker(new URL("./parse.worker.ts", import.meta.url), { type: "module" });
worker.postMessage({ csv: rawText });
worker.onmessage = (event: MessageEvent<{ rows: number }>) => {
render(event.data.rows);
};// parse.worker.ts — no `window`, no `document`, no DOM.
self.onmessage = (event: MessageEvent<{ csv: string }>) => {
const rows: number = parseCsv(event.data.csv); // Takes 400ms. Nobody is blocked by it.
self.postMessage({ rows });
};What crossing the boundary costs
Messages are copied, not shared, using the structured clone algorithm (the browser's deep copy for messages). That copy is real work on both sides, and it happens on the main thread. So sending a 50 MB array to a worker can cost more than the calculation you moved.
| Mechanism | Cost | Use when |
|---|---|---|
postMessage(object) | Full structured clone, both sides | Small messages — parameters and results |
Transferables (ArrayBuffer, ImageBitmap) | Near zero; ownership moves | Large binary payloads |
SharedArrayBuffer | Zero copy, genuinely shared memory | Heavy numeric work, and only with the right COOP/COEP headers |
Transferring instead of copying:
// The second argument lists what to transfer. `buffer` is unusable here afterwards.
worker.postMessage({ buffer }, [buffer]);Structured clone handles most things: objects, arrays, Map, Set, Date, ArrayBuffer, even cyclic references. It cannot handle functions, class identity, or DOM nodes. A class instance arrives as a plain object with its methods gone. That is the failure people hit first.
Making it bearable
Raw postMessage turns a function call into a protocol: an id, a message type, a listener, and a map of pending promises. Comlink wraps that in a proxy, so the worker looks like an async module.
import * as Comlink from "comlink";
// The worker's exported API, callable as if it were local.
const api = Comlink.wrap<{ parseCsv(csv: string): number }>(
new Worker(new URL("./parse.worker.ts", import.meta.url), { type: "module" }),
);
const rows: number = await api.parseCsv(rawText);When to Use It
The test is not "is this slow" but "does this block a frame, and can it leave the DOM behind".
| Work | Worker? | Why |
|---|---|---|
| Parsing a large CSV, JSON or log file | ✅ | Pure computation over a string, no DOM |
| Image resizing, canvas filters, video frames | ✅ | Transferable payloads, genuinely expensive |
| Search indexing, fuzzy matching, diffing | ✅ | CPU-bound and interruptible |
| Cryptography, compression, WASM workloads | ✅ | The classic case |
| Fetching data | ❌ | fetch is already off the main thread; only the parsing is yours |
| Anything that touches the DOM | ❌ | There is no DOM in a worker |
| Work under ~5ms | ❌ | The message round trip costs more than the work |
The INP connection
INP (Interaction to Next Paint) measures the worst delay between an interaction and the next frame. Long tasks are the main cause. The two fixes are the same fix at different scales: yield, or move it. Splitting a task with scheduler.yield() lets the browser handle input between the slices. Moving it to a worker removes it from the main thread entirely. Use a worker when the work is so big that yielding would take dozens of slices.
⚠️ A worker is not free. Each one is a real thread with its own heap, a few megabytes before it does anything. Spawn one per kind of work and keep it, or use a small pool. Do not create one per call.
Common Mistakes
❌ Moving work that was never the problem. Profile first. Most "slow" pages are slow because of rendering, layout thrash or a 2 MB bundle, and a worker helps with none of those. The trace tells you whether the long task is your function or the browser's layout.
❌ Sending the whole object graph. Send what the worker needs and return what the caller needs. Picture a worker that receives the full application state and returns a full copy. It pays the clone cost twice on the main thread, the very thread you were trying to protect.
❌ Expecting class instances to survive. Structured clone copies data, not prototypes. Send plain objects and rehydrate on the other side if you need methods.
❌ Forgetting terminate(). A worker lives until it is terminated or the page unloads. A component that spawns one per mount and never terminates leaks a thread each time.
❌ Assuming SharedArrayBuffer is available. It requires cross-origin isolation, set with the COOP and COEP headers. Turning those on can break third-party embeds and analytics. It is a deployment decision, not a code decision.
🔑 Key Takeaways
- The main thread runs your JavaScript, does layout and paint, and handles input, one at a time. So a long function means a frozen interface.
- A worker has no DOM, so it suits pure computation: parsing, indexing, image and crypto work.
- Messages are structured-cloned, which costs real time on the main thread. For large payloads, transfer
ArrayBuffers instead of copying them. - Class instances lose their prototypes crossing the boundary, and functions cannot cross at all.
- Every worker is a thread with its own heap: pool them, reuse them, and terminate them in teardown.
Interview Questions
Q: A table freezes for half a second when a 20 MB CSV is uploaded. Walk through the fix.
Profile first. Confirm the long task is the parse, not rendering 50,000 rows. Those need different fixes, and doing the wrong one wastes a day. If it is the parse, move it into a module worker. Send the file as an ArrayBuffer transferable, so the main thread does not pay a clone cost. Return only what the view needs: the visible page of rows, not the parsed whole. If rendering is the real problem, virtualise the table, and the worker changes nothing.
Q: What can and cannot cross the worker boundary?
Structured clone handles plain data: objects, arrays, Map, Set, Date, typed arrays, and cyclic references. It cannot carry functions, and it drops prototypes, so a class instance arrives as a plain object with no methods. DOM nodes cannot cross at all. Transfer large binary data rather than cloning it. That moves ownership and makes the buffer unusable on the sending side.
Q: When is a worker the wrong answer to a slow interaction?
When the work is short, when it touches the DOM, or when the bottleneck is rendering rather than scripting. Under about five milliseconds, the round trip costs more than the work. And many "janky" pages are janky because of layout thrash or an oversized bundle. A worker cannot help with either. Adding one only hides the real cause behind a more complicated architecture.
Q: How does moving work to a worker relate to INP?
INP is the delay between an interaction and the frame that responds to it. The usual cause is a long task holding the main thread. A worker removes the task from that thread entirely, so the browser handles the interaction at the next frame. For shorter work, the alternative is yielding. It breaks one long task into slices, and the browser handles input between them. Same goal, two scales.
What to Read Next
- The Event Loop — why one long function blocks everything, in detail
- Core Web Vitals — INP, and what a long task does to the number
- The Observer APIs — the other way to stop doing work on every frame