Node.js · Performance · Backend · JavaScript
Moving CPU-Heavy Work off the Main Thread with Node.js Worker Threads
When to use worker_threads in Node.js, how they differ from child processes and cluster, building a worker pool, sharing data, and keeping the event loop free.

Node.js is fast at I/O, but CPU-heavy work (computing a big report, image processing, compression, hashing) blocks the main thread and makes everyone on the server wait. I covered how to detect that in the Node.js event loop explained; this article covers one of the fixes, worker threads.
Options: workers, child processes, cluster
| Method | When |
|---|---|
worker_threads | CPU-heavy JavaScript in the same process; cheap to start, can share memory |
child_process | Running an external program (ffmpeg, adb) or full isolation |
cluster / multiple processes | Spreading HTTP traffic across cores; scales requests, not a single CPU task |
Rule of thumb: multiple processes to scale requests, workers to take one heavy task off the main thread.
A simple worker
// hash-worker.js
import { parentPort } from 'node:worker_threads';
import { createHash } from 'node:crypto';
parentPort.on('message', ({ id, payload }) => {
const digest = createHash('sha256').update(payload).digest('hex');
parentPort.postMessage({ id, digest });
});
// main.js
import { Worker } from 'node:worker_threads';
const worker = new Worker(new URL('./hash-worker.js', import.meta.url));
worker.on('message', ({ id, digest }) => console.log(id, digest));
worker.postMessage({ id: 1, payload: 'large data' });
Don't start a worker per request
Starting a worker costs tens of milliseconds and the memory of a separate V8 instance. Spawning one per request gives back what you gained. Instead build a pool with one worker per core and dispatch jobs through a queue. A mature library such as piscina is usually better than writing your own:
import Piscina from 'piscina';
const pool = new Piscina({ filename: new URL('./report-worker.js', import.meta.url).href });
app.post('/reports', async (req, res) => {
const report = await pool.run(req.body);
res.json(report);
});
The cost of moving data
postMessage copies data (structured clone). Copying big objects back and forth can eat the time the worker saved. Options:
- Transferables: hand over an
ArrayBufferwithout copying:postMessage(buffer, [buffer]). - SharedArrayBuffer: the main thread and the worker read the same memory; use
Atomicsfor coordination. - Send less: send a file path instead of the file and let the worker read it.
Errors and lifecycle
- An uncaught error inside a worker fires its
errorevent; with a pool, the matching promise rejects. Always handle it. - Add timeouts and cancellation (AbortSignal) for long jobs.
- Close the pool cleanly on shutdown and decide what happens to unfinished work.
When a queue beats a worker
If a job takes seconds or minutes (large video processing, for example), keeping the user waiting on an HTTP request is fragile. Put the job on a job queue (such as BullMQ) and process it in separate processes. Jobs survive restarts, progress can be reported and scaling out gets easier.
Conclusion
Worker threads are Node.js's built-in way to do CPU-heavy work without blocking the event loop. Use a pool, keep data transfer small, and consider a job queue for long-running work.


