Web Workers in Bun

From the Bun cheat sheet ยท Workers & Concurrency ยท verified Jul 2026

Web Workers

Run CPU-intensive tasks in parallel threads

typescript
// main.ts
const worker = new Worker("./worker.ts");
worker.postMessage({ data: [1, 2, 3] });
worker.onmessage = (e) => {
  console.log("Result:", e.data);
};

// worker.ts
self.onmessage = (e) => {
  const result = e.data.data.map((n) => n * 2);
  self.postMessage(result);
};
๐Ÿ’ก Bun Workers support TypeScript directly โ€” no build step needed for worker files
โšก Use navigator.hardwareConcurrency to create an optimal number of workers for the CPU
๐Ÿ“Œ Workers run in separate threads with isolated memory โ€” communicate via postMessage
๐ŸŸข Use workers for CPU-intensive tasks (parsing, compression, crypto) to keep the main thread responsive
workersconcurrencythreadsparallel
Back to the full Bun cheat sheet