Skip to article content
FrontendXLesson 14 / 20

Web Workers

Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.

advanced · about 40 minutes · 2 exercises

  • Predict
  • Explain

What you will be able to do

#
  • Choose between dedicated, shared, and service workers.
  • Pass data with structured clone and transferables.
  • Explain worker limitations.

Overview

#

A web worker runs JavaScript on a background thread so the main thread stays responsive.

Types

  • Dedicated worker: owned by one page.
  • Shared worker: shared by same-origin pages.
  • Service worker: a network proxy, not a general compute worker.

Messaging

Workers communicate with postMessage and the message event. Data is copied with the structured clone algorithm. Transferable objects such as ArrayBuffer are moved instead of copied, which is fast but detaches the source.

snippet.js
// main.js
const worker = new Worker('worker.js');
worker.postMessage({ numbers: [1, 2, 3] });
worker.onmessage = (event) => console.log(event.data);

// worker.js
self.onmessage = (event) => {
  const sum = event.data.numbers.reduce((a, b) => a + b, 0);
  self.postMessage(sum);
};

Limits

Workers have no DOM access. They can use fetch, IndexedDB, and many Web APIs. Module workers use type: 'module'.

Questions to carry forward

  • When is transferring an ArrayBuffer better than copying?
  • How do module workers differ?
  • Why is a service worker a poor compute worker?

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    After transferring an ArrayBuffer to a worker, what happens to the original buffer in the main thread?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.javascript
    worker.postMessage(buffer, [buffer]);
    console.log(buffer.byteLength);
    Choose the best answer
  2. Exercise 2Explain

    Explain when to use a web worker instead of splitting work with setTimeout.

    Reveal the reference answer
    reference.text
    Use a worker for CPU-bound work that would block the main thread. setTimeout still runs on the main thread, so it cannot overlap rendering or input handling with the work.

    Authored expected output: An answer about moving CPU work off the main thread.

    setTimeout only reschedules on the same thread. A worker runs on a separate thread and keeps the main thread free for rendering and input.

Common misconceptions

#
  • Workers can access the DOM.
  • postMessage always copies data.
  • A service worker is the right tool for CPU-heavy computation.

Follow-up practice

#
  • browser-internals-devtools-performance

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.