Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Web Workers allow you to run JavaScript in background threads, separate from the main execution thread. This prevents heavy computations from blocking the UI, keeping your application responsive even during intensive operations.
Run code in parallel without blocking the main thread
Communicate between threads using postMessage
UI remains responsive during heavy operations
// worker.js (Worker file)
self.onmessage = function(e) {
const name = e.data;
self.postMessage('Hello, ' + name + '!');
};
// main.js (Main thread)
const worker = new Worker('worker.js');
// Send message to worker
worker.postMessage('Alice');
// Receive message from worker
worker.onmessage = function(e) {
console.log(e.data); // "Hello, Alice!"
};
// Terminate worker when done
worker.terminate();// fibonacci-worker.js
self.onmessage = function(e) {
const n = e.data;
function fibonacci(num) {
if (num <= 1) return num;
return fibonacci(num - 1) + fibonacci(num - 2);
}
const result = fibonacci(n);
self.postMessage(result);
};
// main.js
const worker = new Worker('fibonacci-worker.js');
worker.postMessage(40);
worker.onmessage = function(e) {
console.log('Fibonacci result:', e.data);
worker.terminate();
};
worker.onerror = function(error) {
console.error('Worker error:', error.message);
};// filter-worker.js
self.onmessage = function(e) {
const { data, minPrice, maxPrice } = e.data;
// Filter products by price range
const filtered = data.filter(product => {
return product.price >= minPrice && product.price <= maxPrice;
});
// Sort by price
filtered.sort((a, b) => a.price - b.price);
self.postMessage(filtered);
};
// main.js
const worker = new Worker('filter-worker.js');
const products = [
{ name: 'Product 1', price: 100 },
{ name: 'Product 2', price: 200 },
{ name: 'Product 3', price: 50 },
// ... thousands more products
];
worker.postMessage({
data: products,
minPrice: 50,
maxPrice: 150
});
worker.onmessage = function(e) {
console.log('Filtered products:', e.data);
displayProducts(e.data);
worker.terminate();
};// safe-worker.js
self.onmessage = function(e) {
try {
const result = performCalculation(e.data);
self.postMessage({ success: true, result });
} catch (error) {
self.postMessage({
success: false,
error: error.message
});
}
};
// main.js
const worker = new Worker('safe-worker.js');
worker.postMessage(data);
worker.onmessage = function(e) {
if (e.data.success) {
console.log('Success:', e.data.result);
} else {
console.error('Worker error:', e.data.error);
}
};
worker.onerror = function(error) {
console.error('Worker crashed:', error.message);
console.error('Line:', error.lineno);
console.error('File:', error.filename);
};Data processing, encryption, image manipulation, large calculations
Call worker.terminate() when done to free memory
Implement both try/catch in worker and onerror in main thread
Message passing overhead isn't worth it for tasks under 50ms
Run heavy computations without blocking UI
Communicate via postMessage/onmessage
Workers cannot manipulate the DOM
Clean up workers to free memory