Web Worker
A Web Worker is a browser API that runs JavaScript code on a background thread, separate from the main UI thread, so CPU-intensive tasks don't block page rendering or user interaction.
Definition
A Web Worker is a browser API that runs JavaScript code on a background thread, separate from the main UI thread, so CPU-intensive tasks don't block page rendering or user interaction.
Overview
JavaScript in the browser normally runs on a single main thread that also handles rendering, layout, and user input — so a long-running computation there will freeze the page. Web Workers solve this by spinning up a separate thread with its own execution context, allowing scripts to run heavy computation in parallel. Communication between the main thread and a worker happens via message passing (postMessage and onmessage), since workers don't share memory directly with the main thread and don't have access to the DOM. There are a few worker variants: dedicated workers are tied to a single script/page instance; shared workers can be accessed by multiple browsing contexts (tabs) from the same origin; and Service Worker is a specialized worker type focused on network interception, caching, and background tasks like push notifications, rather than raw computation. Because workers run in an isolated context without DOM access, they're best suited to tasks that are computationally heavy but don't need to touch the page directly — parsing large files, image or video processing, complex data transformations, or running machine learning inference in the browser. Modern frameworks and build tools increasingly offer ergonomic wrappers (like Comlink) that make worker communication feel closer to a normal function call rather than raw message passing. A worker can itself use IndexedDB for structured storage or cooperate with a Service Worker that handles network caching, keeping heavy computation and network interception both off the main thread.
Key Concepts
- Runs JavaScript on a separate background thread from the main UI thread
- Prevents CPU-intensive tasks from freezing page rendering and interaction
- Communicates with the main thread via asynchronous message passing
- No direct access to the DOM from within the worker
- Dedicated workers serve one page; shared workers can serve multiple tabs
- Distinct from service workers, which focus on caching and network interception
- Well suited to computation-heavy tasks like parsing, image processing, or ML inference
Use Cases
Frequently Asked Questions
From the Blog
What Does a Social Worker Do? A Complete Overview
A social worker helps individuals, families, and communities cope with challenges by connecting them to resources, providing counseling, and advocating on their behalf. The work spans healthcare, schools, child welfare, and community settings.
Read More AI & TechnologyEmployee Engagement: What It Is and Why It Matters
Employee engagement is the level of emotional commitment a worker has to their organization's goals, measured through motivation, discretionary effort, and retention. This guide explains what drives it and how teams can build it deliberately.
Read More ProgrammingGo concurrency patterns: goroutines, channels and select
Use Go's concurrency patterns the way they were designed. Worker pools, fan-in, pipelines with a done channel and select with timeouts are all answers to one question: who closes this channel, and who is left blocked if nobody does.
Read More