Creating a Web Worker
Spawn a background thread for work.
Why Web Workers?
JavaScript runs on a single main thread. A long calculation blocks everything: the UI freezes, clicks stop responding. Web Workers run scripts on a background thread, keeping the page responsive.
Creating a Worker
Spawn a worker by passing a script URL to the Worker constructor. The browser loads that file and runs it on a separate thread.
const worker = new Worker('worker.js');
// The code in worker.js now runs on its own thread.All lessons in this course
- Creating a Web Worker
- Messaging with postMessage
- Transferable Objects
- Use Cases and Limitations