Back to Playground
Performance

Web Workers for Heavy Computation

Offload CPU-intensive tasks to background threads without blocking the main UI thread.

Web WorkersJavaScriptThreading

Demo coming soon...

Code

// worker.js
self.onmessage = (e) => {
  const result = heavyComputation(e.data)
  self.postMessage(result)
}

// main.js
const worker = new Worker('worker.js')

worker.postMessage({ data: largeDataset })

worker.onmessage = (e) => {
  console.log('Result:', e.data)
  updateUI(e.data)
}