# Web Worker

A web worker runs JavaScript in a background browser agent so CPU-heavy work can avoid blocking a page's main event loop.

- Type: term
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/web-worker/>)

## What it means

A web worker is a browser execution context that runs a script separately from a document's main agent. Dedicated workers communicate with their creator through messages, while shared workers can communicate with multiple same-origin contexts. Workers lack direct DOM access and use a different set of APIs; service workers are a specialized event-driven worker category with a distinct lifecycle.

## Why it matters

Moving substantial computation away from the main thread can preserve input and rendering responsiveness. The boundary also forces explicit messaging and data ownership, with startup, transfer, memory, and coordination costs.

## When to use it

Use a worker for measurable CPU-heavy parsing, image processing, search, simulation, or other independent computation. Keep small or DOM-dependent work on the main thread when messaging overhead and complexity would exceed the responsiveness benefit.

## How it works

1. Create a dedicated or shared worker from a script URL or module.
2. Send inputs through messages, transferring ownership of large transferable objects where appropriate.
3. The worker processes tasks in its own global scope and event loop without accessing the DOM.
4. It posts results, progress, or errors back to the page.
5. The application handles cancellation, stale results, failures, and worker termination.

## A practical example

A photo editor moves image decoding and filter calculations into a dedicated worker. It transfers pixel buffers instead of copying them, tags requests so stale results are ignored, and leaves the main thread available for pointer input.

## Where it breaks

Workers do not make algorithms faster automatically, cannot manipulate the DOM directly, and add startup and communication overhead. Structured cloning can copy large data, shared memory requires stronger isolation, and too many workers compete for device resources.

## Common mistakes

- Moving trivial work to a worker without measuring messaging overhead.
- Sending large objects repeatedly instead of considering transferable data.
- Expecting a worker to read or update DOM elements.
- Applying late worker results after the user has changed or cancelled the task.

## Sources

1. [Web Workers API](<https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API>) — MDN Web Docs · Official technical documentation
2. [HTML Standard: Web Workers](<https://html.spec.whatwg.org/multipage/workers.html>) — WHATWG · Living web standard

## Related topics

- [Browser Event Loop](<https://wiki.soroush.uk/topic/browser-event-loop/>)
- [Service Worker](<https://wiki.soroush.uk/topic/service-worker/>)
- [Client-Side Rendering \(CSR\)](<https://wiki.soroush.uk/topic/client-side-rendering/>)
