JavaScript is single-threaded, but Web Workers let you run code in background threads. At Google, we offload heavy computation to workers.
Dedicated Workers
Shared Workers
Shared across multiple tabs/windows of the same origin. Perfect for shared state like WebSocket connections.
Transferable Objects
By default, postMessage copies data (structured clone). For large ArrayBuffers, use transfer to move ownership — zero-copy, instant.
Comlink: Simplified Worker API
What Workers CAN'T Do
- Access the DOM
- Use window, document, or parent
- Access synchronous XHR (don't anyway)
Use Cases
- Image/video processing
- Large dataset sorting/filtering
- Encryption/hashing
- Syntax highlighting (Monaco editor uses workers)
- WebAssembly computation