Skip to content
md.mudassir
← all writing

· 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.

javascriptperformance

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

Main thread
processing data

With a web worker

no blocking

Main thread
Worker
processing data

The consequences

  1. Poor responsiveness. Users hate waiting. A blocked main thread means slower responses to clicks and interactions.
  2. 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.

say hello

Got an idea that should be a product?

I'm always up for a good product conversation — a role, a collaboration, or just comparing notes on shipping. Open to interesting product conversations.

I usually reply within a couple of days.

Local time
— · Bengaluru, IST
Status
Open to interesting product conversations