I reran my Node.js worker-thread benchmark behind a local HTTP endpoint. With CPU work on the main thread, median /ping took 1.8 s. With a four-worker pool, it took 0.5 ms.
The original benchmark timed only the batch. It also hid a worker with no work, and an older split that ran two jobs as five.
I used Node.js 22.21 on a 10-core Apple M4 and tested Node.js 20.20, 22.23 and 24.21 in Docker. Times are medians of three to five runs. Wall time measures elapsed time; process CPU time adds work across threads. Repeat runs agreed within 10% except where noted and for sub-millisecond pings.
1. The benchmark as written: 4×, and one worker with nothing to do
First I ran the original scripts unchanged:
| Run | wall time | CPU time |
|---|---|---|
| main.js: 5 jobs on the main thread | 2,334 ms | 2.31 s |
| index.js: 5 jobs, 6 workers | 575 ms | 2.85 s |
That is four times faster. Then I printed the jobs each worker received:
const jobsPerWorker = jobs.length / numWorkers; // 5 / 6 = 0.8333… // worker 0: jobs.slice(0, 0.8333) -> slice(0, 0) -> [] // worker 1: jobs.slice(0.8333, 1.6667) -> slice(0, 1) -> [1e9] // ... // jobs per worker: [0, 1, 1, 1, 1, 1]
slice() truncates fractional indexes. The first worker did nothing; the other five still produced the correct total of 5,000,000,000.
A job took 530–565 ms on a worker under load, against about 465 ms alone on the main thread, and the process used more CPU in total. Five jobs on five cores gave 4×, not 5×.
2. The version I had commented out: one letter, 2.5 times the work
The older version split jobs with this helper:
function chunkify(array, n) {
let chunks = [];
for (let i = n; i > 0; i--) {
chunks.push(array.slice(0, Math.ceil(array.length / i)));
}
return chunks;
}slice() copies without removing. Each pass starts at index 0 again, and the last chunk contains the whole array. splice() removes the returned items:
slice (as written) splice 2 jobs, 4 workers [1, 1, 1, 2] = 5 jobs [1, 1, 0, 0] = 2 jobs 8 jobs, 4 workers [2, 3, 4, 8] = 17 jobs [2, 2, 2, 2] = 8 jobs
I ran both splits with two jobs:
| Run | wall time | CPU time |
|---|---|---|
| 2 jobs on the main thread | 915 ms | 914 ms |
| 4 workers, chunkify with slice | 991 ms | 2,579 ms |
| 4 workers, chunkify with splice | 486 ms | 1,001 ms |
With the slice bug, four workers were slower than none. Had I stopped at that version, my note would have said worker threads are not worth it. The benchmark printed only “completed” and a time. Printing the total would have shown 5,000,000,000 for two jobs of a billion, and CPU time at 2.8 times the main thread’s would have shown the wasted work.
3. “The number of workers you want”: the cores decide, and os.cpus() does not know your limit
I varied the worker count while keeping the workload fixed: 480 jobs of ten million increments.
| Workers | wall time | speedup | CPU time |
|---|---|---|---|
| main thread | 2,266 ms | 1.0× | – |
| 1 | 2,241 ms | 1.0× | 2,244 ms |
| 2 | 1,155 ms | 2.0× | 2,314 ms |
| 4 | 640 ms | 3.5× | 2,556 ms |
| 6 | 506 ms | 4.5× | 2,914 ms |
| 8 | 415 ms | 5.5× | 3,211 ms |
| 10 | 394 ms | 5.8× | 3,420 ms |
| 12 | 390 ms | 5.8× | 3,466 ms |
| 16 | 398 ms | 5.7× | 3,563 ms |
| 32 | 447 ms | 5.1× | 4,019 ms |
The speedup reached about 5.8× on this machine. From 8 to 16 workers, the fastest count changed between runs; 32 workers took longer than 10 and used more CPU.
I then checked what the runtime reports inside a Docker VM with two CPUs:
os.cpus() os.availableParallelism() docker run (any Node) Node 20.20 Node 22.23 Node 24.21 --cpus=1 2 2 1 1 --cpus=1.5 2 2 1 1 --cpus=0.9 2 2 2 1 --cpus=0.5 2 2 2 1 --cpuset-cpus=0 2 1 1 1
The tested Node.js 20 build respected the cpuset but ignored the quota. The tested 22 build rounded fractional quotas down and treated a result of zero as no limit. The tested 24 build reported one CPU for the sub-CPU limit. The difference comes from how libuv handles that zero. I did not test Kubernetes.
At a one-CPU limit, one worker took 474 ms, two took 518 ms and four took 592 ms. Size the pool from os.availableParallelism(), never os.cpus().length, and print it once inside the container you deploy. On Node.js 20, or on 22 with a limit below one CPU, set the size yourself.
4. What a worker buys on a server: an event loop that keeps answering
I sent eight CPU-heavy /report requests at once and called /ping every 50 ms until they finished:
| Where /report runs | all 8 done | /ping median | /ping max |
|---|---|---|---|
| main thread | 3,661 ms | 1,774 ms | 3,559 ms |
| pool of 4 workers | 1,053 ms | 0.5 ms | 0.8 ms |
| pool of 8 workers | 664 ms | 0.8 ms | 5.2 ms |
At 10 ms between pings, 188 of 333 failed with ECONNRESET or EPIPE on the main-thread version. None failed with a pool of eight.
5. What a worker costs: start-up, memory, and copying
Each worker starts its own V8 isolate, heap and event loop:
new Worker() until its first message ~11 ms (median of 20) memory per idle worker (RSS) ~7.8 MB small message, round trip ~0.01 ms
I compared one worker per request with a reusable pool for 64 concurrent reports:
| Setup | all 64 done | first report | median report | peak RSS |
|---|---|---|---|---|
| pool of 10 workers + queue | 4,757 ms | 692 ms | 2,775 ms | 146 MB |
| new Worker per request | 4,696 ms | 3,216 ms | 4,614 ms | 616 MB |
Both batches finished in roughly the same time. The pool returned the first report at 0.7 s instead of 3.2 s and used much less memory.
postMessage() also has a cost: objects are copied with structured clone.
1,000,000 objects { id, sku, price }
postMessage to a worker and back ~350 ms
main thread blocked inside postMessage ~115 ms (serialising)
summing price on the main thread instead ~8 ms
32 MB Float64Array
copied 4–17 ms
transferred (ownership moves to the worker) ~0.1 ms
SharedArrayBuffer ≤0.1 msCloning a million objects blocked the main thread for 115 ms, longer than summing their prices there. Pass IDs or transferable buffers when the input is large.
6. Sometimes Node already has the threads
Before adding a pool, check whether Node already has an async API. I tested 40 logins using the sync and async versions of crypto.pbkdf2:
| Setup | all 40 done | median login | /ping max |
|---|---|---|---|
| pbkdf2Sync | 1,536 ms | 812 ms | 224 ms |
| pbkdf2, default pool of 4 | 461 ms | 259 ms | 2.7 ms |
| pbkdf2, UV_THREADPOOL_SIZE=8 | 322 ms | 194 ms | 2.5 ms |
Dropping Sync did what a worker pool would do, with no worker code. The async version runs in libuv’s shared pool of four threads, which it also shares with file reads, dns.lookup() and compression. UV_THREADPOOL_SIZE raises the limit; set it when the process starts.
What the notes say now
- A benchmark prints its answer as well as its time, and CPU time next to wall time.
- Check how the work was split before timing it.
- Size the pool from
os.availableParallelism(), checked inside the container. - On a server, the win is the event loop:
/pingwent from 1.8 s to 0.5 ms. - One pool with a queue, created at start-up.
- Send IDs or transferable buffers instead of object graphs.
- Before writing a worker, look for the async API.
The 4× in my notes was correct, and it was the least useful number that benchmark gave me. On a server the question is what every other request waits for while the batch runs.