Learn JavaScript
❯ async.workers

Web Workers (True Parallelism) in JavaScript

$concept

Web Workers run JavaScript on real background threads: new Worker("work.js") executes off the main thread, communicating via postMessage/onmessage with structured-cloneable data (transferables move buffers zero-copy). The UI stays fluid while workers crunch β€” true parallelism beyond the event loop's concurrency.

Workers share nothing (no DOM access, isolated globals like importScripts/fetch) β€” all coordination flows through messages. SharedArrayBuffer plus Atomics enables shared-memory threading where headers allow; otherwise, message-passing keeps the model race-free by construction.

$how it works
1

Threads without shared memory

new Worker("work.js") runs separately β€” main thread free for rendering and input throughout.

2

Message everything

postMessage sends, onmessage receives β€” structured-cloned data, transferables for zero-copy buffers.

3

No DOM inside

Workers see timers, fetch, and IndexedDB β€” never document or window. Compute there, render here.

4

Terminate deliberately

worker.terminate() ends background work β€” orphaned workers burn CPU invisibly.

$example
// main.js:
// let w = new Worker("calc.js");
// w.postMessage(1000000);
// w.onmessage = (e) => console.log("sum:", e.data);
// calc.js:
// onmessage = (e) => { let s = 0; for (let i = 0; i < e.data; i++) s += i; postMessage(s); };
Output
(sum prints while the page stays interactive)
1The main thread dispatches a million-iteration sum and continues rendering immediately.
2The worker crunches off-thread, posting the result back when done.
3No jank, no frozen input β€” parallelism the event loop alone cannot provide.
$use cases
Heavy computation (parsing, crypto, image processing) off the UI thread
Zero-copy buffer transfers for large datasets
Terminating workers when tasks complete or components unmount
SharedArrayBuffer threading where cross-origin isolation permits

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.