How it works
The explanation, with examples you can run.
Seeing the queue
Six expensive hashes, four threads: the first four finish together, the last two have to wait for a free thread.
// pool.mjs (run with: node pool.mjs, then UV_THREADPOOL_SIZE=6 node pool.mjs)import { pbkdf2 } from "node:crypto" const start = performance.now()for (let i = 1; i <= 6; i++) { pbkdf2("secret", "salt", 500_000, 64, "sha512", () => { console.log(`hash ${i} done after ${Math.round(performance.now() - start)} ms`) })}With the default pool you will see two groups of finish times; with six threads (on a machine with at least six cores) they finish together.
Things to know
- The pool is shared by everything in the process. A burst of slow
dns.lookupcalls (which everyhttp.get("https://name...")does unless you pass an IP) can delay unrelated file reads. - The
...Syncfunctions (readFileSync,pbkdf2Sync) don't use the pool at all: they block the main thread, which is fine at start-up and harmful inside a request handler. - A bigger pool helps when work waits on disk or the OS; for CPU-heavy JavaScript of your own, the pool can't help, but worker threads can.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.