100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Web

Web Worker

IntermediateTechnique9.4K learners

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

Parsing or processing large files (CSV, JSON, images) without freezing the UI
Running client-side machine learning inference in the browser
Performing complex data transformations or calculations in dashboards
Encoding or decoding audio and video on the client
Real-time data processing for visualizations and games
Offloading cryptographic operations from the main thread

Frequently Asked Questions