Node.js module
worker_threads
The 'node:worker_threads' module enables multithreaded execution by spawning Worker threads that run JavaScript in isolated contexts.
Features include message passing, transferable objects, SharedArrayBuffer support, and performance measurement, allowing compute-intensive tasks to be offloaded from the main event loop.
Works in Bun · notes
Core worker creation and communication works. Several worker options (stdio, resourceLimits, etc.) and some advanced methods related to transferable objects and heap snapshots are not supported.
Functions 8
- worker_threads.getEnvironmentData()
Within a worker thread,
worker.getEnvironmentData()returns a clone of data passed to the spawning thread'sworker.setEnvironmentData(). - worker_threads.isMarkedAsUntransferable()
Check if an object is marked as not transferable with markAsUntransferable.
- worker_threads.markAsUncloneable()
Mark an object as not cloneable.
- worker_threads.markAsUntransferable()
Mark an object as not transferable.
- worker_threads.moveMessagePortToContext()
Transfer a
MessagePortto a differentvmContext. - worker_threads.postMessageToThread()
Sends a value to another worker, identified by its thread ID.
- worker_threads.receiveMessageOnPort()
Receive a single message from a given
MessagePort. - worker_threads.setEnvironmentData()
The
worker.setEnvironmentData()API sets the content ofworker.getEnvironmentData()in the current thread and all newWorkerinstances spawned from the current c…
Classes 1
- class worker_threads.Worker
The
Workerclass represents an independent JavaScript execution thread.
Interfaces 16
- interface BroadcastChannel
EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.
- interface BroadcastChannelEventMap
- interface Lock
- interface LockGrantedCallback
- interface LockInfo
- interface LockManager
- interface LockManagerSnapshot
- interface LockOptions
- interface MessageChannel
- interface MessagePort
The
NodeEventTargetis a Node.js-specific extension toEventTargetthat emulates a subset of theEventEmitterAPI. - interface MessagePortEventMap
- interface ResourceLimits
- interface StructuredSerializeOptions
- interface WorkerEventMap
- interface WorkerOptions
- interface WorkerPerformance
Types 3
Variables 12
- const worker_threads.BroadcastChannel
- const worker_threads.isInternalThread
- const worker_threads.isMainThread
- const worker_threads.locks
- const worker_threads.MessageChannel
- const worker_threads.MessagePort
- const worker_threads.parentPort
- const worker_threads.resourceLimits
- const worker_threads.SHARE_ENV
- const worker_threads.threadId
- const worker_threads.threadName
- const worker_threads.workerData