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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Service workersA background script sits between page and network and answers from a cache. Intermediate
- Concurrency vs parallelismTaking turns on one core versus truly running at once on several. Intermediate
- Race conditionsTwo threads update one counter and an update vanishes, unless a lock steps in. Intermediate
Spotted a mistake on this page?