· 2 min read · originally on LinkedIn ↗
The power of web workers in JavaScript
Why blocking the main thread is a UX problem, not just a performance one — and how web workers fix it.
Why is blocking the main thread in JavaScript such a big performance and UX issue? When heavy computations or time-consuming tasks tie up the main thread, the whole app feels sluggish. The main thread is responsible for handling user interactions, rendering updates and more — block it, and everything else has to wait, causing delays and freezes.
Without a worker
UI blocked
With a web worker
no blocking
The consequences
- Poor responsiveness. Users hate waiting. A blocked main thread means slower responses to clicks and interactions.
- Stuttering animations. Blocking the main thread makes animations jumpy and unappealing.
Enter web workers
- Parallel processing. Web workers run in the background, independent of the main thread, so they can handle heavy computation without locking up the interface.
- Improved performance. Offloading tasks to workers keeps the main thread responsive, which improves the overall feel of your web app.
- Real multithreading. Workers bring genuine concurrency to JavaScript — a game-changer for complex, CPU-heavy tasks.
The pattern itself is small: move the pure, data-crunching function into a worker file, post the data to it, and await the result.
// heavy.worker.js
self.onmessage = (e) => {
const result = expensiveTransform(e.data);
self.postMessage(result);
};
// main.js
const worker = new Worker(new URL("./heavy.worker.js", import.meta.url), { type: "module" });
worker.onmessage = (e) => render(e.data);
worker.postMessage(rows);For a deeper, production version of this — including a promise wrapper, request IDs and avoiding copy costs — see Keep the main thread boring.
Written by Mohammed Mudassir, senior software engineer in Bengaluru. Turning ideas into scalable products.