Skip to content
IT glossary
FrontendUpdated December 3, 2026

Web Worker

A browser feature that runs JavaScript on a separate thread. Moving slow computations off the page’s main thread helps keep the interface responsive to clicks, typing, and scrolling.

Why use a Web Worker?

Imagine opening a large CSV file in a browser. If the page parses the whole file in one long JavaScript task, it may stop responding to input until parsing finishes. A Web Worker can do the parsing while the main thread stays available to handle input and update the page. The goal is usually better responsiveness, not faster parsing.

How workers communicate

new Worker(url) starts a dedicated worker running the script at that URL. The worker has its own global scope and event loop. It can use APIs such as fetch and timers, but it has no window or document. If its result needs to appear on screen, the page must update the DOM.

Both sides send data with postMessage() and receive it through message events. The receiver reads the payload from event.data. The browser normally copies values using the structured clone algorithm. That works for arrays, maps, and many other data types, but not for functions or DOM nodes. When sending a large ArrayBuffer, you can put it in the transfer list to move ownership instead of copying its bytes. The sender can no longer use that buffer after the transfer.

Example: count values above a threshold

The page sends measurements to a worker. The worker scans them and returns only the count, which the page displays.

// main.js
const worker = new Worker('./count-worker.js');
worker.onmessage = ({ data: count }) => {
  document.querySelector('#result').textContent = count;
  worker.terminate(); // This worker is only needed once
};
worker.postMessage({ readings: [12, 41, 35, 8], limit: 30 });

// count-worker.js (a separate file)
self.onmessage = ({ data: { readings, limit } }) => {
  let count = 0;
  for (const value of readings) {
    if (value > limit) count++;
  }
  self.postMessage(count);
};

Four values aren't enough to justify a worker; the example shows the message flow. In a real application, profile the workload first. Worker startup and data copying can outweigh the benefits for small jobs. If jobs arrive repeatedly, reuse a worker rather than starting one for each request.

Dedicated, shared, and service workers

A dedicated worker belongs to the context that created it. A SharedWorker can serve multiple same-origin pages, each communicating through a message port. A Service Worker is for a different kind of work: it can intercept network requests and serve cached resources for offline use. The browser starts and stops it as needed, so it isn't a place to keep a computation running continuously.

What comes up in interviews

A common question is whether async and await can replace a worker. They can make waiting for I/O non-blocking, but they don't move a CPU-heavy loop to another thread. Even a loop that runs after an await still blocks the main thread if that's where the function runs. To make the trade-off concrete, explain which computation you'd move, how much data would cross the thread boundary, and what work would remain on the main thread.