Skip to main content

Lesson 8 of 9

Web Workers (Background Processing)

Web Workers run JavaScript in a background thread, separate from the main UI thread. Heavy computations (data processing, image manipulation, sorting large arrays) can run in a Worker without freezing the page or blocking user interactions. Workers communicate with the main thread via message passing.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Web Worker Architecture

Web Workers run in a separate thread:

Main Thread (UI) Worker Thread (Background)
โ”œโ”€โ”€ DOM access โœ… โ”œโ”€โ”€ DOM access โŒ
โ”œโ”€โ”€ UI rendering โœ… โ”œโ”€โ”€ Heavy computation โœ…
โ”œโ”€โ”€ postMessage() ----โ†’ โ”œโ”€โ”€ onmessage (receives)
โ”œโ”€โ”€ onmessage (receives) โ†---- postMessage()
โ””โ”€โ”€ User interactions โ””โ”€โ”€ Data processing

Usage:
1. const worker = new Worker('worker.js');
2. worker.postMessage(data); - Send data to worker
3. worker.onmessage = (e) => { /* handle result */ };

In worker.js:
self.onmessage = (e) => {
const result = heavyCalculation(e.data);
self.postMessage(result);
};

Workers CANNOT:
โ€ข Access the DOM (no document, no window)
โ€ข Access parent scope variables directly

Workers CAN:
โ€ข fetch() API, setTimeout, IndexedDB
โ€ข Run CPU-intensive tasks without blocking UI

Diagram
Loading diagramโ€ฆ

Web Workers run intensive computations on background threads to keep UI fluid

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done