JavaScript and TypeScript course ยท Module 10: TypeScript in Practice
Web Workers for Heavy Computations
In this lesson7
Imagine that the park lab is sequencing T-Rex DNA while the control room freezes: the cameras stop and the alarm button does not respond. That is how a browser behaves when heavy computations land on the main thread. Web Workers are a technology that lets you run JavaScript in separate threads, independently of the main UI (User Interface) thread, so the interface stays responsive.
What you will learn:
- move computations to a separate thread and talk to it through
postMessage - recognize the limits of a worker and tell Dedicated, Shared and Service Workers apart
- build a worker pool and share memory through
SharedArrayBuffer - measure performance and handle worker failures
Why Are Web Workers Needed?
The Problem of JavaScript's Single-Threaded Nature
Your code runs on a single main thread, which also handles clicks, scrolling and painting the page. See what a long loop does to it:
// Problem: Blocking computations on the main thread
function heavyCalculation(n) {
console.log('Starting heavy computations...');
// Simulation of intensive computations
let result = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < 1000000; j++) {
result += Math.sqrt(i * j);
}
}
console.log('Computations completed');
return result;
}
// This will block the UI for several seconds!
document.getElementById('calculate-button').addEventListener('click', () => {
const result = heavyCalculation(5000);
document.getElementById('result').textContent = result;
});
// During computations the user cannot:
// - Click other buttons
// - Scroll the page
// - Use animations
// - Type text in formsAs long as the computation runs, the event loop cannot handle a click or an animation frame. This is the problem Web Workers solve.
Performance Comparison
A worker does not compute faster, because it is the same JavaScript engine. The difference is who has to wait. This benchmark sketch compares both approaches:
// Benchmark: Main Thread vs Web Worker
const performanceTest = {
// Test without Web Worker - blocks UI
async testMainThread() {
console.time('Main Thread Calculation');
const start = performance.now();
const result = this.calculatePrimes(100000);
const end = performance.now();
console.timeEnd('Main Thread Calculation');
return {
result: result.length,
time: end - start,
blocked: true
};
},
// Test with Web Worker - does not block UI
async testWebWorker() {
console.time('Web Worker Calculation');
const start = performance.now();
const result = await this.calculatePrimesWorker(100000);
const end = performance.now();
console.timeEnd('Web Worker Calculation');
return {
result: result.length,
time: end - start,
blocked: false
};
},
calculatePrimes(max) {
const primes = [];
for (let i = 2; i <= max; i++) {
if (this.isPrime(i)) {
primes.push(i);
}
}
return primes;
},
isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
};The time may be similar, but with a worker the interface keeps responding the whole time.
Basic Web Worker Implementations
Creating a Simple Web Worker
You create a worker with the new Worker('worker.js') constructor, which runs the script in a separate thread. The order matters: first set up the onmessage listener, then send data with postMessage, and finally end the work with the terminate() method:
// main.js - main thread
class WebWorkerManager {
constructor() {
this.worker = null;
this.setupWorker();
}
setupWorker() {
// Check Web Workers support
if (typeof Worker !== 'undefined') {
this.worker = new Worker('worker.js');
// Handle messages from the worker
this.worker.onmessage = (event) => {
this.handleWorkerMessage(event.data);
};
// Handle errors
this.worker.onerror = (error) => {
console.error('Worker error:', error);
};
console.log('Web Worker has been created');
} else {
console.warn('Web Workers are not supported in this browser');
}
}
// Send a task to the worker
sendTask(taskType, data) {
if (this.worker) {
this.worker.postMessage({
type: taskType,
data: data,
timestamp: Date.now()
});
}
}
// Handle responses from the worker
handleWorkerMessage(message) {
switch (message.type) {
case 'CALCULATION_COMPLETE':
this.displayResult(message.result);
break;
case 'PROGRESS_UPDATE':
this.updateProgress(message.progress);
break;
case 'ERROR':
this.handleError(message.error);
break;
}
}
// Terminate the worker
terminateWorker() {
if (this.worker) {
this.worker.terminate();
this.worker = null;
console.log('Web Worker has been terminated');
}
}
}
// Usage
const workerManager = new WebWorkerManager();
// Send a computation task
document.getElementById('start-calculation').addEventListener('click', () => {
workerManager.sendTask('CALCULATE_PRIMES', { max: 100000 });
});You read the response from event.data, by assigning a function to onmessage or through addEventListener('message', ...). The data is not shared: the browser copies it with the structured clone algorithm, so you cannot send functions or DOM elements.
Implementing worker.js
On the other side, the worker listens with self.onmessage, replies with self.postMessage and can report progress:
// worker.js - code executed in a separate thread
class WorkerCalculations {
constructor() {
this.setupMessageHandler();
}
setupMessageHandler() {
// Main message handler from the main thread
self.onmessage = (event) => {
const { type, data, timestamp } = event.data;
try {
this.handleTask(type, data, timestamp);
} catch (error) {
this.sendError(error.message);
}
};
}
handleTask(type, data, timestamp) {
switch (type) {
case 'CALCULATE_PRIMES':
this.calculatePrimes(data.max);
break;
case 'PROCESS_IMAGE':
this.processImage(data.imageData);
break;
case 'SORT_LARGE_ARRAY':
this.sortArray(data.array);
break;
case 'MONTE_CARLO_PI':
this.calculatePiMonteCarlo(data.iterations);
break;
default:
this.sendError(`Unknown task type: ${type}`);
}
}
// Calculate prime numbers with progress reporting
calculatePrimes(max) {
const primes = [];
const updateInterval = Math.floor(max / 100); // Update every 1%
for (let i = 2; i <= max; i++) {
if (this.isPrime(i)) {
primes.push(i);
}
// Report progress
if (i % updateInterval === 0) {
const progress = (i / max) * 100;
this.sendProgress(progress);
}
}
this.sendResult('CALCULATION_COMPLETE', {
primes: primes,
count: primes.length,
executionTime: performance.now()
});
}
isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
// Image processing (filtering, transformations)
processImage(imageData) {
const { data, width, height, filterType } = imageData;
switch (filterType) {
case 'grayscale':
this.applyGrayscaleFilter(data);
break;
case 'blur':
this.applyBlurFilter(data, width, height);
break;
case 'edge':
this.applyEdgeDetection(data, width, height);
break;
}
this.sendResult('IMAGE_PROCESSED', {
processedData: data,
filterApplied: filterType
});
}
applyGrayscaleFilter(data) {
for (let i = 0; i < data.length; i += 4) {
const gray = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114;
data[i] = gray; // Red
data[i + 1] = gray; // Green
data[i + 2] = gray; // Blue
// Alpha (i + 3) remains unchanged
}
}
// Sorting large arrays
sortArray(array) {
const startTime = performance.now();
// Merge sort implementation for better performance
const sortedArray = this.mergeSort(array);
const endTime = performance.now();
this.sendResult('SORT_COMPLETE', {
sortedArray: sortedArray,
originalLength: array.length,
executionTime: endTime - startTime
});
}
mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = this.mergeSort(arr.slice(0, mid));
const right = this.mergeSort(arr.slice(mid));
return this.merge(left, right);
}
merge(left, right) {
const result = [];
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] <= right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
return result.concat(left.slice(leftIndex), right.slice(rightIndex));
}
// Calculate Pi using Monte Carlo method
calculatePiMonteCarlo(iterations) {
let pointsInCircle = 0;
const updateInterval = Math.floor(iterations / 100);
for (let i = 0; i < iterations; i++) {
const x = Math.random() * 2 - 1; // -1 to 1
const y = Math.random() * 2 - 1; // -1 to 1
if (x * x + y * y <= 1) {
pointsInCircle++;
}
if (i % updateInterval === 0) {
const currentPi = (pointsInCircle / i) * 4;
const progress = (i / iterations) * 100;
this.sendProgress(progress, { currentPi, iteration: i });
}
}
const finalPi = (pointsInCircle / iterations) * 4;
this.sendResult('PI_CALCULATION_COMPLETE', {
pi: finalPi,
iterations: iterations,
accuracy: Math.abs(Math.PI - finalPi)
});
}
// Helper methods for communication
sendResult(type, result) {
self.postMessage({
type: type,
result: result,
timestamp: Date.now()
});
}
sendProgress(progress, additionalData = {}) {
self.postMessage({
type: 'PROGRESS_UPDATE',
progress: progress,
...additionalData,
timestamp: Date.now()
});
}
sendError(errorMessage) {
self.postMessage({
type: 'ERROR',
error: errorMessage,
timestamp: Date.now()
});
}
}
// Initialize worker
new WorkerCalculations();A worker has its own global scope (self) and no access to the DOM, document or window. It can use fetch, XMLHttpRequest, setTimeout, setInterval and IndexedDB, though. So the lifecycle looks like this: creation, postMessage(), work in the worker thread and receiving the result in onmessage.
Advanced Web Worker Patterns
Worker Pool for Parallel Computations
A pool creates several workers, usually as many as the number of cores reported by navigator.hardwareConcurrency, and distributes tasks from a queue:
// WorkerPool.js - Managing multiple workers
class WorkerPool {
constructor(workerScript, poolSize = navigator.hardwareConcurrency || 4) {
this.workerScript = workerScript;
this.poolSize = poolSize;
this.workers = [];
this.taskQueue = [];
this.activeTasksCount = 0;
this.initializePool();
}
initializePool() {
for (let i = 0; i < this.poolSize; i++) {
const worker = {
instance: new Worker(this.workerScript),
busy: false,
id: i
};
worker.instance.onmessage = (event) => {
this.handleWorkerMessage(worker, event.data);
};
worker.instance.onerror = (error) => {
this.handleWorkerError(worker, error);
};
this.workers.push(worker);
}
console.log(`Worker pool initialized with ${this.poolSize} workers`);
}
// Execute a task using the pool
execute(taskData) {
return new Promise((resolve, reject) => {
const task = {
data: taskData,
resolve: resolve,
reject: reject,
id: this.generateTaskId()
};
const availableWorker = this.getAvailableWorker();
if (availableWorker) {
this.assignTaskToWorker(availableWorker, task);
} else {
this.taskQueue.push(task);
}
});
}
getAvailableWorker() {
return this.workers.find(worker => !worker.busy);
}
assignTaskToWorker(worker, task) {
worker.busy = true;
worker.currentTask = task;
this.activeTasksCount++;
worker.instance.postMessage({
taskId: task.id,
...task.data
});
}
handleWorkerMessage(worker, message) {
const task = worker.currentTask;
if (message.type === 'TASK_COMPLETE') {
task.resolve(message.result);
this.releaseWorker(worker);
} else if (message.type === 'TASK_ERROR') {
task.reject(new Error(message.error));
this.releaseWorker(worker);
} else if (message.type === 'PROGRESS_UPDATE') {
// Pass progress update (optional)
if (task.onProgress) {
task.onProgress(message.progress);
}
}
}
releaseWorker(worker) {
worker.busy = false;
worker.currentTask = null;
this.activeTasksCount--;
// Check if there are tasks in the queue
if (this.taskQueue.length > 0) {
const nextTask = this.taskQueue.shift();
this.assignTaskToWorker(worker, nextTask);
}
}
handleWorkerError(worker, error) {
console.error(`Worker ${worker.id} error:`, error);
if (worker.currentTask) {
worker.currentTask.reject(error);
this.releaseWorker(worker);
}
}
// Parallel processing of large datasets
async processInParallel(dataArray, chunkProcessor) {
const chunkSize = Math.ceil(dataArray.length / this.poolSize);
const chunks = [];
// Split data into chunks
for (let i = 0; i < dataArray.length; i += chunkSize) {
chunks.push(dataArray.slice(i, i + chunkSize));
}
// Process chunks in parallel
const promises = chunks.map(chunk =>
this.execute({
type: 'PROCESS_CHUNK',
chunk: chunk,
processor: chunkProcessor.toString()
})
);
const results = await Promise.all(promises);
// Combine results
return results.flat();
}
generateTaskId() {
return `task_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
// Terminate all workers
terminate() {
this.workers.forEach(worker => {
worker.instance.terminate();
});
this.workers = [];
this.taskQueue = [];
console.log('Worker pool terminated');
}
// Pool statistics
getStats() {
return {
poolSize: this.poolSize,
activeWorkers: this.workers.filter(w => w.busy).length,
queueLength: this.taskQueue.length,
activeTasks: this.activeTasksCount
};
}
}
// Using Worker Pool
const workerPool = new WorkerPool('calculation-worker.js', 8);
// Example of parallel processing
async function processLargeDataset() {
const largeArray = Array.from({ length: 1000000 }, (_, i) => Math.random() * 1000);
try {
const results = await workerPool.processInParallel(largeArray, (chunk) => {
// Processing function will be executed in a worker
return chunk.map(x => Math.sqrt(x * x + 1));
});
console.log('Processing completed:', results.length);
console.log('Stats:', workerPool.getStats());
} catch (error) {
console.error('Processing error:', error);
}
}When all workers are busy, a task waits in the queue, and releaseWorker hands out the next one right away. chunkProcessor reaches the worker as text, because postMessage cannot carry functions.
Shared Array Buffer for Efficient Communication
SharedArrayBuffer is memory visible to several threads at once, so the workers write the results of matrix multiplication straight into a shared buffer, without copying:
// SharedArrayBufferWorker.js - Shared memory between workers
class SharedArrayBufferManager {
constructor() {
this.checkSupport();
}
checkSupport() {
if (typeof SharedArrayBuffer === 'undefined') {
console.warn('SharedArrayBuffer is not supported');
this.fallbackToTransferableObjects();
return false;
}
return true;
}
// Create shared memory for matrix computations
createSharedMatrixBuffer(rows, cols) {
const bufferSize = rows * cols * Float64Array.BYTES_PER_ELEMENT;
const sharedBuffer = new SharedArrayBuffer(bufferSize);
const matrix = new Float64Array(sharedBuffer);
return {
buffer: sharedBuffer,
matrix: matrix,
rows: rows,
cols: cols,
// Helper methods (they use the matrix variable, because this in an arrow function is the manager)
get: (row, col) => {
return matrix[row * cols + col];
},
set: (row, col, value) => {
matrix[row * cols + col] = value;
},
fill: (value) => {
matrix.fill(value);
}
};
}
// Parallel matrix multiplication
async parallelMatrixMultiply(matrixA, matrixB, workerCount = 4) {
const rowsA = matrixA.rows;
const colsA = matrixA.cols;
const colsB = matrixB.cols;
// Create shared result matrix
const resultMatrix = this.createSharedMatrixBuffer(rowsA, colsB);
// Distribute work among workers
const rowsPerWorker = Math.ceil(rowsA / workerCount);
const workers = [];
for (let i = 0; i < workerCount; i++) {
const startRow = i * rowsPerWorker;
const endRow = Math.min(startRow + rowsPerWorker, rowsA);
if (startRow < rowsA) {
const worker = new Worker('matrix-worker.js');
const promise = new Promise((resolve, reject) => {
worker.onmessage = (event) => {
if (event.data.type === 'MATRIX_COMPLETE') {
worker.terminate();
resolve(event.data.result);
}
};
worker.onerror = reject;
});
worker.postMessage({
type: 'MULTIPLY_MATRIX_ROWS',
matrixABuffer: matrixA.buffer,
matrixBBuffer: matrixB.buffer,
resultBuffer: resultMatrix.buffer,
dimensions: {
rowsA: rowsA,
colsA: colsA,
colsB: colsB
},
rowRange: { start: startRow, end: endRow }
});
workers.push(promise);
}
}
await Promise.all(workers);
return resultMatrix;
}
fallbackToTransferableObjects() {
console.log('Using Transferable Objects as an alternative');
// Implementation with Transferable Objects
}
}
// matrix-worker.js - Worker for matrix computations
self.onmessage = function(event) {
const { type, matrixABuffer, matrixBBuffer, resultBuffer, dimensions, rowRange } = event.data;
if (type === 'MULTIPLY_MATRIX_ROWS') {
const matrixA = new Float64Array(matrixABuffer);
const matrixB = new Float64Array(matrixBBuffer);
const result = new Float64Array(resultBuffer);
const { rowsA, colsA, colsB } = dimensions;
const { start, end } = rowRange;
// Matrix multiplication for assigned rows
for (let i = start; i < end; i++) {
for (let j = 0; j < colsB; j++) {
let sum = 0;
for (let k = 0; k < colsA; k++) {
sum += matrixA[i * colsA + k] * matrixB[k * colsB + j];
}
result[i * colsB + j] = sum;
}
}
self.postMessage({
type: 'MATRIX_COMPLETE',
result: `Rows ${start}-${end} completed`
});
}
};Each worker computes different rows, so they do not get in each other's way. When threads change the same cells, use Atomics (for example Atomics.add and Atomics.wait), which operate atomically. Browsers expose SharedArrayBuffer only to pages isolated with the Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp headers.
Service Worker for Background Processing
Besides the regular (dedicated) worker there are two more kinds. A Shared Worker, created with new SharedWorker(), is shared by tabs and windows from the same origin. A Service Worker sits between the page and the network and handles caching, offline mode and background sync:
// ServiceWorkerManager.js - Managing long-running background tasks
class ServiceWorkerManager {
constructor() {
this.serviceWorker = null;
this.messageChannel = null;
this.setupServiceWorker();
}
async setupServiceWorker() {
if ('serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.register('background-worker.js');
// Wait for service worker activation
await navigator.serviceWorker.ready;
this.serviceWorker = registration.active || registration.waiting || registration.installing;
this.setupMessageChannel();
console.log('Service Worker registered and ready');
} catch (error) {
console.error('Service Worker registration error:', error);
}
}
}
setupMessageChannel() {
this.messageChannel = new MessageChannel();
// Handle messages from the Service Worker
this.messageChannel.port1.onmessage = (event) => {
this.handleServiceWorkerMessage(event.data);
};
// Send port to the Service Worker
navigator.serviceWorker.controller?.postMessage(
{ type: 'INIT_PORT' },
[this.messageChannel.port2]
);
}
// Schedule a long-running task for the Service Worker
scheduleBackgroundTask(taskConfig) {
if (this.messageChannel) {
this.messageChannel.port1.postMessage({
type: 'SCHEDULE_TASK',
task: {
id: this.generateTaskId(),
...taskConfig,
scheduledAt: Date.now()
}
});
}
}
// Example: Periodic data synchronization
scheduleDataSync(interval = 300000) { // 5 minutes
this.scheduleBackgroundTask({
type: 'DATA_SYNC',
interval: interval,
endpoint: '/api/sync',
recurring: true
});
}
// Example: Background file processing
scheduleFileProcessing(files) {
files.forEach(file => {
this.scheduleBackgroundTask({
type: 'FILE_PROCESSING',
fileName: file.name,
fileData: file,
processor: 'image-compression'
});
});
}
handleServiceWorkerMessage(message) {
switch (message.type) {
case 'TASK_COMPLETED':
this.onTaskCompleted(message.task, message.result);
break;
case 'TASK_FAILED':
this.onTaskFailed(message.task, message.error);
break;
case 'SYNC_COMPLETED':
this.onSyncCompleted(message.result);
break;
}
}
onTaskCompleted(task, result) {
console.log(`Task ${task.id} completed:`, result);
// Update UI or application state
this.updateApplicationState(task, result);
}
generateTaskId() {
return `bg_task_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
}
// background-worker.js - Service Worker
class BackgroundTaskProcessor {
constructor() {
this.activeTasks = new Map();
this.messagePort = null;
this.setupEventListeners();
}
setupEventListeners() {
self.addEventListener('message', (event) => {
if (event.data.type === 'INIT_PORT') {
this.messagePort = event.ports[0];
this.setupPortListener();
}
});
// Background Sync
self.addEventListener('sync', (event) => {
if (event.tag === 'background-data-sync') {
event.waitUntil(this.performDataSync());
}
});
}
setupPortListener() {
this.messagePort.onmessage = (event) => {
this.handleTask(event.data);
};
}
async handleTask(message) {
if (message.type === 'SCHEDULE_TASK') {
const task = message.task;
try {
const result = await this.executeTask(task);
this.messagePort.postMessage({
type: 'TASK_COMPLETED',
task: task,
result: result
});
} catch (error) {
this.messagePort.postMessage({
type: 'TASK_FAILED',
task: task,
error: error.message
});
}
}
}
async executeTask(task) {
switch (task.type) {
case 'DATA_SYNC':
return await this.performDataSync(task);
case 'FILE_PROCESSING':
return await this.processFile(task);
case 'BATCH_CALCULATION':
return await this.performBatchCalculation(task);
default:
throw new Error(`Unknown task type: ${task.type}`);
}
}
async performDataSync(task) {
try {
const response = await fetch(task.endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ timestamp: Date.now() })
});
if (!response.ok) {
throw new Error(`Sync failed: ${response.status}`);
}
const data = await response.json();
// Store in IndexedDB or Cache API
await this.storeDataLocally(data);
return { success: true, syncedRecords: data.length };
} catch (error) {
throw new Error(`Data sync failed: ${error.message}`);
}
}
async processFile(task) {
// File processing simulation
const { fileName, processor } = task;
switch (processor) {
case 'image-compression':
return await this.compressImage(task.fileData);
case 'video-transcoding':
return await this.transcodeVideo(task.fileData);
default:
throw new Error(`Unknown processor: ${processor}`);
}
}
async storeDataLocally(data) {
// IndexedDB storage implementation
return new Promise((resolve, reject) => {
const request = indexedDB.open('AppDatabase', 1);
request.onsuccess = (event) => {
const db = event.target.result;
const transaction = db.transaction(['syncedData'], 'readwrite');
const store = transaction.objectStore('syncedData');
store.put({ id: 'latest', data: data, timestamp: Date.now() });
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
};
request.onerror = () => reject(request.error);
});
}
}
new BackgroundTaskProcessor();A Service Worker is not suited to long computations, though, because the browser puts it to sleep when it is not handling events. On the first visit navigator.serviceWorker.controller can be null, hence the ?. operator in the code.
Performance Monitoring and Debugging
The monitor wraps postMessage and onmessage to record the duration of every task:
// WorkerPerformanceMonitor.js
class WorkerPerformanceMonitor {
constructor() {
this.metrics = {
taskExecutionTimes: [],
memoryUsage: [],
errorRates: {},
throughput: []
};
this.startTime = performance.now();
}
// Monitor worker performance
monitorWorkerPerformance(worker, taskId) {
const startTime = performance.now();
let memoryBefore = null;
// Memory measurement (if available)
if (performance.memory) {
memoryBefore = performance.memory.usedJSHeapSize;
}
const originalPostMessage = worker.postMessage.bind(worker);
const originalOnMessage = worker.onmessage;
// Intercept postMessage
worker.postMessage = (message) => {
console.log(`[Worker] Sending task ${taskId}:`, message);
originalPostMessage(message);
};
// Intercept onmessage
worker.onmessage = (event) => {
const endTime = performance.now();
const executionTime = endTime - startTime;
// Record execution time metric
this.metrics.taskExecutionTimes.push({
taskId: taskId,
executionTime: executionTime,
timestamp: Date.now()
});
// Memory measurement after execution
if (performance.memory && memoryBefore) {
const memoryAfter = performance.memory.usedJSHeapSize;
const memoryDelta = memoryAfter - memoryBefore;
this.metrics.memoryUsage.push({
taskId: taskId,
memoryDelta: memoryDelta,
timestamp: Date.now()
});
}
console.log(`[Worker] Task ${taskId} completed in ${executionTime.toFixed(2)}ms`);
if (originalOnMessage) {
originalOnMessage(event);
}
};
return worker;
}
// Performance analysis
generatePerformanceReport() {
const report = {
totalRuntime: performance.now() - this.startTime,
taskCount: this.metrics.taskExecutionTimes.length,
averageExecutionTime: this.calculateAverageExecutionTime(),
memoryTrends: this.analyzeMemoryTrends(),
throughputAnalysis: this.calculateThroughput(),
recommendations: this.generateRecommendations()
};
return report;
}
calculateAverageExecutionTime() {
if (this.metrics.taskExecutionTimes.length === 0) return 0;
const total = this.metrics.taskExecutionTimes.reduce(
(sum, metric) => sum + metric.executionTime, 0
);
return total / this.metrics.taskExecutionTimes.length;
}
analyzeMemoryTrends() {
if (this.metrics.memoryUsage.length === 0) return null;
const memoryDeltas = this.metrics.memoryUsage.map(m => m.memoryDelta);
const averageMemoryDelta = memoryDeltas.reduce((a, b) => a + b, 0) / memoryDeltas.length;
return {
averageMemoryDelta: averageMemoryDelta,
maxMemoryDelta: Math.max(...memoryDeltas),
minMemoryDelta: Math.min(...memoryDeltas),
potentialMemoryLeak: averageMemoryDelta > 1024 * 1024 // > 1MB average
};
}
calculateThroughput() {
const timeWindow = 60000; // 1 minute
const now = Date.now();
const recentTasks = this.metrics.taskExecutionTimes.filter(
task => now - task.timestamp < timeWindow
);
return {
tasksPerMinute: recentTasks.length,
averageTaskTime: recentTasks.length > 0
? recentTasks.reduce((sum, task) => sum + task.executionTime, 0) / recentTasks.length
: 0
};
}
generateRecommendations() {
const recommendations = [];
const avgTime = this.calculateAverageExecutionTime();
const memoryTrends = this.analyzeMemoryTrends();
if (avgTime > 5000) { // > 5 seconds
recommendations.push('Consider splitting long-running tasks into smaller chunks');
}
if (memoryTrends?.potentialMemoryLeak) {
recommendations.push('Potential memory leak detected - review memory management');
}
if (this.metrics.taskExecutionTimes.length > 1000) {
recommendations.push('Large number of tasks - consider implementing a Worker Pool');
}
return recommendations;
}
}
// Monitoring usage
const performanceMonitor = new WorkerPerformanceMonitor();
// Monitor a specific worker
const worker = new Worker('calculation-worker.js');
const monitoredWorker = performanceMonitor.monitorWorkerPerformance(worker, 'task_001');
// Generate report after some time
setTimeout(() => {
const report = performanceMonitor.generatePerformanceReport();
console.log('Performance Report:', report);
}, 30000);performance.memory is a non-standard API available only in Chromium, so treat these measurements as approximate.
Best Practices and Optimizations
Optimal Communication with Workers
Every message has a copying cost, so we batch small ones, compress large ones and pass buffers without copying:
// OptimizedWorkerCommunication.js
class OptimizedWorkerCommunication {
constructor() {
this.messageQueue = [];
this.batchSize = 10;
this.batchTimeout = 16; // ~60fps
this.compressionThreshold = 1024; // 1KB
}
// Message batching for better performance
sendBatchedMessage(worker, message) {
this.messageQueue.push({ worker, message });
if (this.messageQueue.length >= this.batchSize) {
this.flushMessageQueue();
} else {
// Flush after timeout if batch is not full
setTimeout(() => {
if (this.messageQueue.length > 0) {
this.flushMessageQueue();
}
}, this.batchTimeout);
}
}
flushMessageQueue() {
const workerMessages = new Map();
// Group messages by workers
this.messageQueue.forEach(({ worker, message }) => {
if (!workerMessages.has(worker)) {
workerMessages.set(worker, []);
}
workerMessages.get(worker).push(message);
});
// Send batched messages
workerMessages.forEach((messages, worker) => {
worker.postMessage({
type: 'BATCH_MESSAGES',
messages: messages,
timestamp: Date.now()
});
});
this.messageQueue = [];
}
// Compress large data before sending
async sendCompressedData(worker, data) {
const serializedData = JSON.stringify(data);
if (serializedData.length > this.compressionThreshold) {
try {
// Use Compression Streams API (if available)
const compressedData = await this.compressData(serializedData);
worker.postMessage({
type: 'COMPRESSED_DATA',
data: compressedData,
compressed: true,
originalSize: serializedData.length
});
} catch (error) {
// Fallback to uncompressed data
worker.postMessage({
type: 'RAW_DATA',
data: data,
compressed: false
});
}
} else {
worker.postMessage({
type: 'RAW_DATA',
data: data,
compressed: false
});
}
}
async compressData(data) {
// Compression implementation (example with Compression Streams)
if ('CompressionStream' in window) {
const stream = new CompressionStream('gzip');
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
writer.write(new TextEncoder().encode(data));
writer.close();
const chunks = [];
let done = false;
while (!done) {
const { value, done: readerDone } = await reader.read();
done = readerDone;
if (value) {
chunks.push(value);
}
}
return new Uint8Array(chunks.reduce((acc, chunk) => [...acc, ...chunk], []));
}
// Fallback - return original data
return new TextEncoder().encode(data);
}
// Transferable Objects for efficient transfer
sendTransferableData(worker, arrayBuffer) {
worker.postMessage({
type: 'TRANSFERABLE_DATA',
data: arrayBuffer
}, [arrayBuffer]); // Transfer ownership
console.log('Transferred ArrayBuffer to worker (zero-copy)');
}
}postMessage(data, [arrayBuffer]) passes the buffer as a transferable object (Transferable): instead of a copy, the ownership changes, and on the main thread the buffer then has a length of 0.
Error Handling and Resilience
A worker can die because of an error in the script. A resilient manager retries tasks with a growing delay (exponential backoff), enforces time limits and recreates dead workers:
// ResilientWorkerManager.js
class ResilientWorkerManager {
constructor(workerScript, options = {}) {
this.workerScript = workerScript;
this.options = {
maxRetries: 3,
retryDelay: 1000,
healthCheckInterval: 30000,
taskTimeout: 60000,
...options
};
this.workers = new Map();
this.failedTasks = new Map();
this.healthCheckTimer = null;
this.startHealthChecking();
}
// Create a worker with error handling
createResilientWorker(workerId) {
const worker = new Worker(this.workerScript);
const workerInfo = {
instance: worker,
id: workerId,
healthy: true,
taskCount: 0,
errorCount: 0,
lastActivity: Date.now(),
activeTasks: new Map()
};
// Error handling
worker.onerror = (error) => {
this.handleWorkerError(workerInfo, error);
};
// Message handling with timeout
worker.onmessage = (event) => {
this.handleWorkerMessage(workerInfo, event.data);
};
this.workers.set(workerId, workerInfo);
return workerInfo;
}
// Execute a task with retry logic
async executeTaskWithRetry(taskData, workerId = null) {
let attempts = 0;
let lastError = null;
while (attempts < this.options.maxRetries) {
try {
const result = await this.executeTask(taskData, workerId);
// Reset failed task counter on success
if (this.failedTasks.has(taskData.id)) {
this.failedTasks.delete(taskData.id);
}
return result;
} catch (error) {
attempts++;
lastError = error;
console.warn(`Task ${taskData.id} failed (attempt ${attempts}/${this.options.maxRetries}):`, error);
if (attempts < this.options.maxRetries) {
// Exponential backoff
const delay = this.options.retryDelay * Math.pow(2, attempts - 1);
await this.sleep(delay);
// Recreate worker if needed
if (error.type === 'WORKER_DIED') {
await this.recreateWorker(workerId);
}
}
}
}
// Record task as failed
this.failedTasks.set(taskData.id, {
taskData: taskData,
attempts: attempts,
lastError: lastError,
failedAt: Date.now()
});
throw new Error(`Task ${taskData.id} failed after ${attempts} attempts: ${lastError.message}`);
}
executeTask(taskData, workerId) {
return new Promise((resolve, reject) => {
const worker = workerId ? this.workers.get(workerId) : this.getHealthyWorker();
if (!worker || !worker.healthy) {
reject(new Error('No healthy worker available'));
return;
}
const taskId = taskData.id || this.generateTaskId();
const timeoutId = setTimeout(() => {
worker.activeTasks.delete(taskId);
reject(new Error(`Task ${taskId} timed out`));
}, this.options.taskTimeout);
// Record task as active
worker.activeTasks.set(taskId, {
resolve: resolve,
reject: reject,
timeoutId: timeoutId,
startTime: Date.now()
});
worker.taskCount++;
worker.lastActivity = Date.now();
worker.instance.postMessage({
...taskData,
taskId: taskId
});
});
}
handleWorkerMessage(workerInfo, message) {
const { taskId, type, result, error } = message;
if (workerInfo.activeTasks.has(taskId)) {
const task = workerInfo.activeTasks.get(taskId);
clearTimeout(task.timeoutId);
workerInfo.activeTasks.delete(taskId);
workerInfo.lastActivity = Date.now();
if (type === 'TASK_SUCCESS') {
task.resolve(result);
} else if (type === 'TASK_ERROR') {
workerInfo.errorCount++;
task.reject(new Error(error));
}
}
}
handleWorkerError(workerInfo, error) {
console.error(`Worker ${workerInfo.id} error:`, error);
workerInfo.healthy = false;
workerInfo.errorCount++;
// Reject all active tasks
workerInfo.activeTasks.forEach(task => {
clearTimeout(task.timeoutId);
task.reject(new Error('Worker died'));
});
workerInfo.activeTasks.clear();
// Auto-restart worker
setTimeout(() => {
this.recreateWorker(workerInfo.id);
}, this.options.retryDelay);
}
async recreateWorker(workerId) {
const oldWorker = this.workers.get(workerId);
if (oldWorker) {
oldWorker.instance.terminate();
this.workers.delete(workerId);
}
console.log(`Recreating worker ${workerId}`);
const newWorker = this.createResilientWorker(workerId);
// Health check for the new worker
await this.waitForWorkerReady(newWorker);
}
waitForWorkerReady(workerInfo, timeout = 5000) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error(`Worker ${workerInfo.id} failed to initialize`));
}, timeout);
// Ping test
const pingTest = () => {
workerInfo.instance.postMessage({
type: 'PING',
timestamp: Date.now()
});
};
const handlePong = (event) => {
if (event.data.type === 'PONG') {
clearTimeout(timeoutId);
workerInfo.instance.removeEventListener('message', handlePong);
workerInfo.healthy = true;
resolve(workerInfo);
}
};
workerInfo.instance.addEventListener('message', handlePong);
pingTest();
});
}
// Periodic worker health checking
startHealthChecking() {
this.healthCheckTimer = setInterval(() => {
this.performHealthCheck();
}, this.options.healthCheckInterval);
}
performHealthCheck() {
this.workers.forEach((workerInfo, workerId) => {
const timeSinceLastActivity = Date.now() - workerInfo.lastActivity;
// Check if worker is responsive
if (timeSinceLastActivity > this.options.healthCheckInterval * 2) {
console.warn(`Worker ${workerId} appears unresponsive`);
this.recreateWorker(workerId);
}
// Check error rate
const errorRate = workerInfo.errorCount / Math.max(workerInfo.taskCount, 1);
if (errorRate > 0.1) { // > 10% error rate
console.warn(`Worker ${workerId} has high error rate: ${(errorRate * 100).toFixed(2)}%`);
}
});
}
getHealthyWorker() {
const healthyWorkers = Array.from(this.workers.values()).filter(w => w.healthy);
if (healthyWorkers.length === 0) {
return null;
}
// Select the worker with the fewest active tasks
return healthyWorkers.reduce((best, current) =>
current.activeTasks.size < best.activeTasks.size ? current : best
);
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
generateTaskId() {
return `task_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
// Resource cleanup
terminate() {
if (this.healthCheckTimer) {
clearInterval(this.healthCheckTimer);
}
this.workers.forEach(workerInfo => {
workerInfo.instance.terminate();
});
this.workers.clear();
}
// System statistics
getSystemStats() {
const workers = Array.from(this.workers.values());
return {
totalWorkers: workers.length,
healthyWorkers: workers.filter(w => w.healthy).length,
totalTasks: workers.reduce((sum, w) => sum + w.taskCount, 0),
activeTasks: workers.reduce((sum, w) => sum + w.activeTasks.size, 0),
totalErrors: workers.reduce((sum, w) => sum + w.errorCount, 0),
failedTasksCount: this.failedTasks.size,
averageErrorRate: workers.length > 0
? (workers.reduce((sum, w) => sum + (w.errorCount / Math.max(w.taskCount, 1)), 0) / workers.length * 100).toFixed(2) + '%'
: '0%'
};
}
}The time limit of each task protects you from a frozen worker, and PING and PONG check whether a new worker responds.
Practical example: 3D rendering
Finally, the renderer splits the screen into 64 by 64 pixel tiles and hands them to a worker pool, which computes the colors with ray tracing:
// 3DRenderingApp.js
class WebWorker3DRenderer {
constructor(canvasId) {
this.canvas = document.getElementById(canvasId);
this.ctx = this.canvas.getContext('2d');
this.workerPool = new WorkerPool('3d-renderer-worker.js', 4);
this.scene = {
objects: [],
camera: { x: 0, y: 0, z: -10 },
lights: [{ x: 5, y: 5, z: -5, intensity: 1 }]
};
this.setupScene();
}
setupScene() {
// Add 3D objects to the scene
this.scene.objects = [
{ type: 'cube', position: { x: 0, y: 0, z: 0 }, rotation: { x: 0, y: 0, z: 0 } },
{ type: 'sphere', position: { x: 3, y: 0, z: 2 }, radius: 1 },
{ type: 'cylinder', position: { x: -3, y: 0, z: 1 }, height: 2, radius: 0.5 }
];
}
async render() {
const startTime = performance.now();
// Split screen into tiles for parallel rendering
const tileSize = 64;
const tilesX = Math.ceil(this.canvas.width / tileSize);
const tilesY = Math.ceil(this.canvas.height / tileSize);
const renderPromises = [];
for (let tileY = 0; tileY < tilesY; tileY++) {
for (let tileX = 0; tileX < tilesX; tileX++) {
const tileData = {
type: 'RENDER_TILE',
tile: {
x: tileX * tileSize,
y: tileY * tileSize,
width: Math.min(tileSize, this.canvas.width - tileX * tileSize),
height: Math.min(tileSize, this.canvas.height - tileY * tileSize)
},
scene: this.scene,
screenDimensions: {
width: this.canvas.width,
height: this.canvas.height
}
};
renderPromises.push(
this.workerPool.execute(tileData).then(result => ({
...result,
tileX: tileX,
tileY: tileY
}))
);
}
}
// Wait for all tiles
const renderedTiles = await Promise.all(renderPromises);
// Compose tiles on canvas
renderedTiles.forEach(tile => {
const imageData = new ImageData(
new Uint8ClampedArray(tile.pixelData),
tile.width,
tile.height
);
this.ctx.putImageData(imageData, tile.x, tile.y);
});
const renderTime = performance.now() - startTime;
console.log(`Frame rendered in ${renderTime.toFixed(2)}ms`);
}
// Scene animation
animate() {
this.scene.objects.forEach(obj => {
if (obj.rotation) {
obj.rotation.y += 0.02;
obj.rotation.x += 0.01;
}
});
this.render().then(() => {
requestAnimationFrame(() => this.animate());
});
}
}
// 3d-renderer-worker.js
class Tile3DRenderer {
constructor() {
self.onmessage = (event) => {
this.handleRenderTask(event.data);
};
}
handleRenderTask(data) {
if (data.type === 'RENDER_TILE') {
const result = this.renderTile(data.tile, data.scene, data.screenDimensions);
self.postMessage({
type: 'TASK_COMPLETE',
result: result
});
}
}
renderTile(tile, scene, screenDimensions) {
const { x, y, width, height } = tile;
const pixelData = new Uint8ClampedArray(width * height * 4);
for (let py = 0; py < height; py++) {
for (let px = 0; px < width; px++) {
const screenX = x + px;
const screenY = y + py;
// Transform screen coordinates to ray
const ray = this.screenToRay(screenX, screenY, screenDimensions, scene.camera);
// Ray tracing
const color = this.traceRay(ray, scene);
const pixelIndex = (py * width + px) * 4;
pixelData[pixelIndex] = color.r; // Red
pixelData[pixelIndex + 1] = color.g; // Green
pixelData[pixelIndex + 2] = color.b; // Blue
pixelData[pixelIndex + 3] = 255; // Alpha
}
}
return {
x: x,
y: y,
width: width,
height: height,
pixelData: Array.from(pixelData)
};
}
screenToRay(screenX, screenY, screenDimensions, camera) {
const normalizedX = (screenX / screenDimensions.width) * 2 - 1;
const normalizedY = 1 - (screenY / screenDimensions.height) * 2;
return {
origin: camera,
direction: {
x: normalizedX,
y: normalizedY,
z: 1
}
};
}
traceRay(ray, scene) {
let closestDistance = Infinity;
let hitColor = { r: 0, g: 0, b: 50 }; // Background
// Check collision with objects
scene.objects.forEach(obj => {
const distance = this.rayObjectIntersection(ray, obj);
if (distance > 0 && distance < closestDistance) {
closestDistance = distance;
hitColor = this.calculateObjectColor(obj, ray, distance, scene.lights);
}
});
return hitColor;
}
rayObjectIntersection(ray, obj) {
switch (obj.type) {
case 'sphere':
return this.raySphereIntersection(ray, obj);
case 'cube':
return this.rayCubeIntersection(ray, obj);
default:
return -1;
}
}
raySphereIntersection(ray, sphere) {
const dx = ray.origin.x - sphere.position.x;
const dy = ray.origin.y - sphere.position.y;
const dz = ray.origin.z - sphere.position.z;
const a = ray.direction.x * ray.direction.x +
ray.direction.y * ray.direction.y +
ray.direction.z * ray.direction.z;
const b = 2 * (dx * ray.direction.x + dy * ray.direction.y + dz * ray.direction.z);
const c = dx * dx + dy * dy + dz * dz - sphere.radius * sphere.radius;
const discriminant = b * b - 4 * a * c;
if (discriminant < 0) return -1;
const t1 = (-b - Math.sqrt(discriminant)) / (2 * a);
const t2 = (-b + Math.sqrt(discriminant)) / (2 * a);
return t1 > 0 ? t1 : (t2 > 0 ? t2 : -1);
}
calculateObjectColor(obj, ray, distance, lights) {
// Simplified lighting
const baseColor = obj.type === 'sphere'
? { r: 255, g: 100, b: 100 }
: { r: 100, g: 255, b: 100 };
// Lighting simulation
const lightIntensity = 0.7 + 0.3 * Math.sin(distance * 0.1);
return {
r: Math.floor(baseColor.r * lightIntensity),
g: Math.floor(baseColor.g * lightIntensity),
b: Math.floor(baseColor.b * lightIntensity)
};
}
}
new Tile3DRenderer();The main thread only assembles the tiles on the canvas. It would be faster to pass the pixel buffers as Transferables instead of converting them to arrays with Array.from.
Summary
Web Workers enable:
- Running heavy computations without blocking the UI
- Parallel processing of large datasets
- Better responsiveness of web applications
- Scalability through a Worker Pool
- Background processing with Service Workers
You will reach for them in mathematical and scientific computations, image and video processing, 3D rendering, sorting and filtering large datasets, compression, cryptography and hashing, and running machine learning models. Start with a single worker in module mode (new Worker('worker.js', { type: 'module' })), and build a pool only when measurements show that it is not enough. A worker simulation waits in the editor below, and in the next lesson we will look into memory and the garbage collector.
Remember: a worker is a separate lab behind glass that works in parallel, but you talk to it only through the postMessage window.
Code for this lesson: index.js
// Web Workers - prime number calculation example
// Main thread
console.log("Jurassic Park - DNA Analytics System");
console.log("Starting heavy calculations without blocking UI\n");
// Simulating a Web Worker (a real worker requires a separate file)
function simulateWorker() {
// Function checking if a number is prime
function isPrime(n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) return false;
}
return true;
}
// Find prime numbers in range
function findPrimes(start, end) {
const primes = [];
for (let i = start; i <= end; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
return primes;
}
return findPrimes;
}
// Main function
async function calculatePrimesInBackground() {
console.log("Starting calculations in the background...");
console.log("The UI stays responsive!\n");
const findPrimes = simulateWorker();
// Simulation of long-running computations
const start = 1;
const end = 10000;
console.time("Calculation time");
const primes = findPrimes(start, end);
console.timeEnd("Calculation time");
console.log(`\nFound ${primes.length} prime numbers`);
console.log(`First 20: ${primes.slice(0, 20).join(', ')}`);
console.log(`Last 20: ${primes.slice(-20).join(', ')}`);
}
// Worker API structure (for reference)
console.log("=== Web Worker API Structure ===");
console.log("1. Creating a worker:");
console.log(" const worker = new Worker('worker.js');");
console.log("\n2. Sending data to the worker:");
console.log(" worker.postMessage({ action: 'calculate', data: 100 });");
console.log("\n3. Receiving results:");
console.log(" worker.onmessage = (event) => console.log(event.data);");
console.log("\n4. Error handling:");
console.log(" worker.onerror = (error) => console.error(error);");
console.log("\n5. Closing the worker:");
console.log(" worker.terminate();\n");
// Run the calculations
calculatePrimesInBackground();
// Demonstration of concurrent UI operation
console.log("\nUI works normally during calculations!");
console.log("Other operations can be performed...");
console.log("Clicks, animations, everything is responsive!");
// Example of a "worker" using a promise
class PrimeWorkerSimulator {
calculate(start, end) {
return new Promise((resolve) => {
setTimeout(() => {
const primes = [];
for (let i = start; i <= end; i++) {
if (this.isPrime(i)) primes.push(i);
}
resolve(primes);
}, 0); // Async execution
});
}
isPrime(n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) return false;
}
return true;
}
}
// Test with a promise-based worker
const workerSim = new PrimeWorkerSimulator();
workerSim.calculate(10000, 11000).then(primes => {
console.log(`\nComputation completed! Found ${primes.length} prime numbers between 10000 and 11000`);
});Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is the main problem that Web Workers solve?
2. Which method do we use to send data to a Web Worker?
These are 2 of 5 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Create a worker for calculating prime numbers.
- Click in order
Arrange the steps of communication with a Web Worker.
- Horizontal ordering
Arrange the elements for creating a Web Worker:
- Click in order
Arrange the elements of creating a Web Worker in the correct order:
- Code editor
Use SharedArrayBuffer for communication between threads.
- Vertical ordering
Arrange the Web Worker lifecycle stages in the correct order:
- Click in order
Arrange the elements of sending a message to a Worker:
Tasks after this lesson
- QuestionSingle-Threaded JavaScript Problems
- QuestionWeb Worker API
- Coding taskImplementing a Web Worker
- QuestionWeb Worker Limitations
- Click in orderCommunication with a Web Worker
- Put in orderWeb Worker Syntax
- QuestionShared Workers vs Dedicated Workers
- Click in orderOrder: Creating a Web Worker
- Coding taskExercise: SharedArrayBuffer and Atomics
- QuestionReceiving Data from a Web Worker
- Put in orderWeb Worker Lifecycle
- Click in orderOrder: postMessage