Documentation Index
Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
Use this file to discover all available pages before exploring further.
Worker lets you start and communicate with a new JavaScript instance running on a separate thread, while sharing I/O resources with the main thread.
Bun implements the Web Workers API with server-side extensions. Like the rest of Bun, workers support CommonJS, ES Modules, TypeScript, JSX, and TSX out of the box — no build step needed.
Creating a worker
Main thread
index.ts
Worker thread
worker.ts
Add
declare var self: Worker; to the top of your worker file to avoid TypeScript errors. Unlike in browsers, you do not need to specify { type: "module" } to use ES Modules in workers.new Worker(url) is resolved relative to the project root. If the file does not exist, an error is thrown immediately at construction time.
Sending and receiving messages
Useworker.postMessage to send messages to the worker, and self.postMessage inside the worker to send messages back. Messages are serialized using the HTML Structured Clone Algorithm.
Performance
Bun includes fast paths forpostMessage that dramatically improve performance for common data types:
- String fast path — posting a pure string bypasses structured clone entirely.
- Simple object fast path — plain objects containing only primitives use an optimized path.
postMessage is 2–241x faster than Node.js for typical payloads.
Worker lifecycle events
”open”
Emitted when the worker is ready to receive messages. Messages sent before this event are automatically queued.”close”
Emitted when the worker has been terminated. TheCloseEvent contains the exit code.
Terminating workers
A worker terminates automatically when its event loop has no more work. To forcefully terminate a worker:process.exit(). This does not terminate the main process.
Managing worker lifetime
By default, an activeWorker keeps the main process alive.
worker.unref()
Decouple the worker’s lifetime from the main process. The main process can exit even if the worker is still running:worker.ref()
Re-attach the worker to the main process lifetime:worker.ref() and worker.unref() are Bun extensions and are not available in browsers.Checking the current thread
UseBun.isMainThread to check whether code is running on the main thread or in a worker:
Sharing environment data
Share data from the main thread to all workers usingsetEnvironmentData and getEnvironmentData from worker_threads:
index.ts
worker.ts
Preloading modules
Use thepreload option to load modules before the worker script starts. This is useful for instrumentation tools like OpenTelemetry, Sentry, or DataDog:
blob: URLs
You can create a worker from an inline string using ablob: URL:
blob: URLs support TypeScript, JSX, and other formats out of the box.
Memory usage
Workers can consume significant memory. Usesmol: true to reduce memory usage at the cost of some performance:
Small instead of Large.
Worker events on the main process
Listen for worker creation events usingprocess.on("worker"):
SharedArrayBuffer and Atomics
For true shared memory between threads, useSharedArrayBuffer with Atomics for synchronization:
index.ts
worker.ts
Node.js worker_threads compatibility
Bun is compatible with Node.js’sworker_threads module: