Web Workers Explained for Frontend Developers
JavaScript in the browser is single-threaded β but modern web apps often need to do heavy work without freezing the UI.
Thatβs where Web Workers come in.
Web Workers allow real parallel execution of JavaScript code in the browser.
π§ The Problem Web Workers Solve
Without workers, heavy computations block the main thread:
button.onclick = () => {
heavyCalculation(); // β UI freezes
};
Symptoms:
- Scrolling jank
- Input lag
- βPage is unresponsiveβ warnings
Why?
π The main thread is responsible for:
- Rendering
- User input
- Running JS
Block it, and everything stops.
π What Is a Web Worker?
A Web Worker is:
- A separate JavaScript thread
- Running in parallel to the main thread
- With its own event loop
- No access to the DOM
This is true parallelism, not just async I/O.
π§± Main Thread vs Worker
| Capability | Main Thread | Web Worker |
|---|---|---|
| Run JS | β | β |
| DOM access | β | β |
| Parallel execution | β | β |
| Network requests | β | β |
| Timers | β | β |
| Blocking UI | β | β (isolated) |
βοΈ Basic Example
worker.js
self.onmessage = (event) => {
const result = event.data * 2;
self.postMessage(result);
};
main.js
const worker = new Worker("worker.js");
worker.postMessage(10);
worker.onmessage = (event) => {
console.log("Result from worker:", event.data);
};
Flow:
Main Thread β postMessage β Worker
ββ Worker responds β postMessage β Main Thread
π Communication Model
Workers communicate via message passing, not shared memory (by default).
- Data is copied using the structured clone algorithm
- No race conditions
- No shared mutable state
worker.postMessage({ numbers: [1, 2, 3] });
β‘ Transferable Objects (Advanced)
To avoid copying large data:
worker.postMessage(buffer, [buffer]);
- Ownership transfers to the worker
- Zero-copy
- Original buffer becomes unusable on main thread
Great for:
- Image processing
- Audio buffers
- Large datasets
π§© Real-World Use Cases
β CPU-heavy work:
- Data processing
- Encryption / hashing
- Image resizing
- PDF parsing
- Chart calculations
β Not for:
- DOM updates
- Simple async calls
- Lightweight logic
βοΈ Using Web Workers with React
useEffect(() => {
const worker = new Worker(new URL("./worker.js", import.meta.url));
worker.postMessage(data);
worker.onmessage = (e) => {
setResult(e.data);
};
return () => worker.terminate();
}, []);
Best practice:
- Create workers lazily
- Terminate when not needed
- Keep worker logic pure
π§ Web Workers vs async/await
| Feature | async/await | Web Worker |
|---|---|---|
| Parallel JS | β | β |
| I/O concurrency | β | β |
| CPU offloading | β | β |
| DOM access | β | β |
π§ͺ Types of Workers
| Type | Description |
|---|---|
| Dedicated Worker | One page β one worker |
| Shared Worker | Multiple tabs share |
| Service Worker | Network proxy / offline |
This article focuses on Dedicated Workers.
β οΈ Common Mistakes
β Doing DOM work in workers
β Spawning too many workers
β Sending huge objects without transferables
β Forgetting to terminate workers
π§ Mental Model
- Main thread = UI brain
- Worker = background CPU
- Messages = mailbox
Workers do the heavy lifting.
Main thread stays smooth.
β Summary
| Concept | Meaning |
|---|---|
| Web Worker | Background JS thread |
| Parallelism | Real CPU parallel execution |
| Communication | postMessage |
| DOM access | Not allowed |
| Best use | Heavy computation |
β¨ Final Takeaway
If async/await gives you concurrency, Web Workers give you parallelism.
Use them when performance truly matters.
Suggested follow-ups
- SharedArrayBuffer & Atomics
- Worker Pools
- OffscreenCanvas
Originally published on Bitlyst.
Top comments (0)