JS Web Workers
const worker = new Worker("worker.js");
worker.postMessage(data);
worker.onmessage = function(event) {
// event.data
};
What Is a Web Worker?
A Web Worker runs JavaScript in a separate browser thread and can handle heavy work away from the main UI thread.
Because a worker runs on its own thread, expensive computations there don't freeze scrolling, animations, or user input on the main page.
उदाहरण: What Is a Web Worker?
// Declare the constant `workerCode`, set to `postMessage("Worker started");`
// Declare the constant `workerCode`, set to `postMessage("Worker started");`
const workerCode = `postMessage("Worker started");`;
// Declare the constant `blob` as a new `Blob` instance
// Declare the constant `blob` as a new `Blob` instance
const blob = new Blob([workerCode], { type: "application/javascript" });
// Declare the constant `worker` as a new `Worker` instance
// Declare the constant `worker` as a new `Worker` instance
const worker = new Worker(URL.createObjectURL(blob));
// Assign `(e) => console.log(e.data)` to `worker.onmessage`
// Assign `(e) => console.log(e.data)` to `worker.onmessage`
worker.onmessage = (e) => console.log(e.data);
postMessage()
The main thread and worker exchange data with postMessage(). Workers don't share memory with the main thread, so all communication happens by sending copies of data back and forth through postMessage(), not by direct variable access.
उदाहरण: postMessage()
// Declare the constant `workerCode`, set to `onmessage = (e) => postMessage("Received: " + e.data);`
// Declare the constant `workerCode`, set to `onmessage = (e) => postMessage("Received: " + e.data);`
const workerCode = `onmessage = (e) => postMessage("Received: " + e.data);`;
// Declare the constant `worker` as a new `Worker` instance
// Declare the constant `worker` as a new `Worker` instance
const worker = new Worker(URL.createObjectURL(new Blob([workerCode])));
// Assign `(e) => console.log(e.data)` to `worker.onmessage`
// Assign `(e) => console.log(e.data)` to `worker.onmessage`
worker.onmessage = (e) => console.log(e.data);
// Call `worker.postMessage("Hello")`
// Call `worker.postMessage("Hello")`
worker.postMessage("Hello");
Worker Errors
Workers can report errors to the main thread with the error event. Listening for the worker's error event lets the main thread catch and handle failures happening inside the worker's separate execution context.
उदाहरण: Worker Errors
// Declare the constant `workerCode`, set to `throw new Error("Worker failed");`
// Declare the constant `workerCode`, set to `throw new Error("Worker failed");`
const workerCode = `throw new Error("Worker failed");`;
// Declare the constant `worker` as a new `Worker` instance
// Declare the constant `worker` as a new `Worker` instance
const worker = new Worker(URL.createObjectURL(new Blob([workerCode])));
// Assign `(e) => console.log("Worker error:", e.message)` to `worker.onerror`
// Assign `(e) => console.log("Worker error:", e.message)` to `worker.onerror`
worker.onerror = (e) => console.log("Worker error:", e.message);
Terminate a Worker
terminate() stops a worker when it is no longer needed. Calling terminate() immediately stops the worker's script entirely, which is important for freeing up resources once its work is genuinely finished.
उदाहरण: Terminate a Worker
const workerCode = `postMessage("Running");`;
const worker = new Worker(URL.createObjectURL(new Blob([workerCode])));
worker.onmessage = (e) => {
console.log(e.data);
worker.terminate(); // stop the worker once done
};
Practical Use
Web Workers are useful for CPU-heavy calculations and data processing that should not block the page. A typical use case is running a large data-processing or image-manipulation task in a worker so the page stays responsive while it happens.
उदाहरण: Practical Use
// Declare the constant `workerCode`, set to `
const workerCode = `
let total = 0;
for (let i = 0; i < 1e6; i++) total += i;
postMessage(total);
`;
// Declare the constant `worker` as a new `Worker` instance
const worker = new Worker(URL.createObjectURL(new Blob([workerCode])));
// Assign `(e) => console.log("Heavy computation result:", e.data)` to `worker.onmessage`
worker.onmessage = (e) => console.log("Heavy computation result:", e.data);
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML