worker threads
The Node.js module for running JavaScript in parallel threads inside one process, each with its own V8 isolate and event loop.
In more detail
Workers suit CPU-bound jobs such as hashing or parsing. They exchange messages with postMessage and can share memory through SharedArrayBuffer. Starting one takes time and memory, so reuse them through a pool.
Related terms
- thread — A path of execution inside a process that the operating system schedules on a processor; one process can have many.
- V8 isolate — A separate instance of the V8 engine with its own heap and garbage collector; objects in one isolate cannot be seen from another.
- MessageChannel — A pair of connected ports for sending messages between threads: what you post on one port arrives as a message event on the other.
- SharedArrayBuffer — A block of raw memory that several threads can read and write at the same time, without copying.
- thread pool — A fixed or bounded set of reusable worker threads that take tasks from a queue, instead of creating a new thread for each task.
Taught in
- Native Module Systems: ESM Loader Hooks vs CommonJS — Node.js Internals & Backend Services
- Worker Threads: worker_threads & SharedArrayBuffer — Node.js Internals & Backend Services
- Thread Pooling: Piscina & CPU-Bound Workload Pools — Node.js Internals & Backend Services