- Assertion testing
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
moduleAPI - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Policies
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Worker threads
- Zlib
Node.js v14.21.3 documentation
Table of contents
- Worker threads
worker.getEnvironmentData(key)worker.isMainThreadworker.markAsUntransferable(object)worker.moveMessagePortToContext(port, contextifiedSandbox)worker.parentPortworker.receiveMessageOnPort(port)worker.resourceLimitsworker.SHARE_ENVworker.setEnvironmentData(key[, value])worker.threadIdworker.workerData- Class:
MessageChannel - Class:
MessagePort - Class:
Workernew Worker(filename[, options])- Event:
'error' - Event:
'exit' - Event:
'message' - Event:
'messageerror' - Event:
'online' worker.getHeapSnapshot()worker.performanceworker.postMessage(value[, transferList])worker.ref()worker.resourceLimitsworker.stderrworker.stdinworker.stdoutworker.terminate()worker.threadIdworker.unref()
- Notes
Worker threads#
Stability: 2 - Stable
Source Code: lib/worker_threads.js
The worker_threads module enables the use of threads that execute JavaScript
in parallel. To access it:
const worker = require('worker_threads');
Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be.
Unlike child_process or cluster, worker_threads can share memory. They do
so by transferring ArrayBuffer instances or sharing SharedArrayBuffer
instances.
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
module.exports = function parseJSAsync