Node Worker Threads
worker_threads is Node's API for extra JavaScript threads in one process. Each worker has its own V8 isolate and event loop; you share work with messages, or with SharedArrayBuffer.
Detailed Explanation
Node's main thread runs your entry script and libuv's I/O. CPU-heavy work (crypto, image, parse, compress) still blocks that thread if you do it there. child_process is another process: separate memory, heavier spawn. worker_threads is another thread: faster spawn, shared optional memory, same process limits.
const { Worker } = require("node:worker_threads");
const worker = new Worker("./job.js", { workerData: { n: 40 } });
workerData is a structured-clone snapshot for startup. After that, parentPort.postMessage / worker.on("message") is the channel. MessageChannel gives extra ports. Transfer lists move ArrayBuffers. SharedArrayBuffer works without browser COOP/COEP.
isMainThread and parentPort let one file be both launcher and worker. worker.terminate() returns a promise. Uncaught errors in the worker emit error on the Worker object and can kill the worker.
Workers are not a substitute for async I/O. fs.promises.readFile already uses the thread pool / libuv. Use workers for JavaScript CPU, or for native addons that block. A pool of workers (size ≈ CPU count) plus a job queue is the usual pattern; spawning a worker per request is a good way to run out of memory.
What it costs you: isolate overhead (a few MB each), clone cost, and complexity around debugging and unhandled rejections. Cluster and child processes isolate crashes better: a worker that writes wild native memory can still take the process down.
Reach for worker threads when a request would otherwise pin the event loop for tens of milliseconds. Reach for child_process when you need a different uid, a different binary, or crash isolation. Reach for the built-in async APIs when the wait is I/O.
Code Examples
main.js
// main.js
const { Worker } = require("node:worker_threads");
function fibAsync(n) {
return new Promise((resolve, reject) => {
const worker = new Worker(
`
const { parentPort, workerData } = require("node:worker_threads");
function fib(x) {
return x <= 1 ? x : fib(x - 1) + fib(x - 2);
}
parentPort.postMessage(fib(workerData.n));
`,
{ eval: true, workerData: { n } },
);
worker.once("message", resolve);
worker.once("error", reject);
});
}
fibAsync(10).then((n) => console.log(n)); // => 55job.js — alternative file form
// job.js — alternative file form
const { parentPort, workerData, isMainThread } = require("node:worker_threads");
if (!isMainThread) {
parentPort.postMessage({ doubled: workerData.n * 2 });
}Common Gotchas
Do not spawn a worker per tiny job
pool them. Construction is cheaper than a process and still expensive next to a function call.
The main event loop is still one thread
a worker helps only if you *move* the CPU work off it.
Uncaught exceptions emit `error`
listen, or the `Worker` object can crash the test / process depending on Node version and flags.