Frontend / Web Workers

Web Workers

A page’s JavaScript shares one thread with layout and painting. See a two-second job freeze that thread, then move it to a worker and watch the page stay smooth.

Intermediate7 steps~6 min
Main thread — runs page script, layout and paint
Worker — a second thread for heavy computing
Page — the DOM; only the main thread sees it
// THE SHORT VERSION

In 30 seconds

In a web page, your JavaScript, layout and painting all share one main thread. If a script computes for two seconds, nothing else happens for two seconds: the page freezes.

A Web Worker is a second thread running its own script. Heavy computing goes there, and the main thread stays free to paint and respond.

The two threads share no variables. They exchange messages, each side getting its own copy of the data. A worker cannot touch the page, so it sends results back for the main thread to display.

Web Workers, step by step

STEPS01 / 07

The 2000 ms job is an invented example. Messages are copied by default; large buffers can instead be transferred, and SharedArrayBuffer allows real shared memory under strict conditions.

STEP 01 / 07 One thread does everything
THE MATH
60 frames per second 1000 ms / 60 ≈ 16.7 ms per frame
  1. Step 1: One thread does everything. A page’s JavaScript, its layout and its painting all take turns on one main thread: one thing at a time, in order. To look smooth a typical screen wants a new frame 60 times a second, so each round of script, layout and paint has roughly 16.7 milliseconds.
  2. Step 2: A long task freezes the page. Blurring a large photo takes 2000 ms of pure computing. While it runs, the thread can do nothing else: no frame is painted and a click made at 0.75 s is only handled at 2.25 s. Zoomed out to seconds, that is about 120 missed frames. The page looks frozen.
  3. Step 3: Start a second thread. A Web Worker is a second thread that the page starts by naming a script file. The browser loads blur.js and runs it alongside the page, with its own event loop and its own variables. Whatever it computes, it does not occupy the main thread.
  4. Step 4: Talk by messages. The two threads cannot call each other’s functions or read each other’s variables. They post messages. The page sends the photo with postMessage, the worker receives a copy in its message handler. Since each side works on its own copy, they cannot corrupt each other’s data.
  5. Step 5: Heavy work, smooth page. The 2000 ms of computing now happens on the worker’s thread. The main thread keeps its 16.7 ms rhythm the whole time: frames are painted, and a click at 0.75 s is handled at 0.75 s. The job is no faster. It has only stopped blocking the page.
  6. Step 6: No DOM in a worker. A worker has no document and no window, so it cannot change the page. When the blur is finished it posts the result back, and a handler on the main thread puts it on screen. Workers do keep the non-visual tools: fetch, timers, IndexedDB, sockets.
  7. Step 7: When a worker pays off. Workers suit long stretches of computing: filtering images, parsing big files, compressing, encrypting, searching. Anything that would hold the main thread for more than about 50 ms is a candidate. Short jobs are not worth the cost of the messages, and waiting on the network never blocks the thread in the first place.
VALUES SO FAR
frame16.7 msbudget
blur····task
missed····cost
threads····setup
message····rule
missed····result
DOM····limit
use for····rule

Spotted a mistake on this page?