Web Workers (True Parallelism) in JavaScript
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.
Threads without shared memory
new Worker("work.js") runs separately β main thread free for rendering and input throughout.
Message everything
postMessage sends, onmessage receives β structured-cloned data, transferables for zero-copy buffers.
No DOM inside
Workers see timers, fetch, and IndexedDB β never document or window. Compute there, render here.
Terminate deliberately
worker.terminate() ends background work β orphaned workers burn CPU invisibly.
// 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); };Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.