CodePark/ questions / Runtime, V8 & libuv
START PRACTICINGPRACTICE
Backend question · Node.js › Runtime, V8 & libuv

With default settings, each pbkdf2 call takes about 300 ms on this machine. How do the five callbacks finish?

const crypto = require("node:crypto");
const start = Date.now();
for (let i = 1; i <= 5; i++) {
  crypto.pbkdf2("pw", "salt", 300000, 64, "sha512", () => {
    console.log(i, Date.now() - start);
  });
}
Four finish at about 300 ms, the fifth at about 600 ms

"Four finish at about 300 ms, the fifth at about 600 ms" is correct. Async pbkdf2 runs on the libuv thread pool. The pool has 4 threads by default. So four hashes run in parallel, and the fifth waits for a free thread.

"They finish one after another, about 300 ms apart" is wrong. The calls do not run one by one, because they do not run on the main thread.


If you set
UV_THREADPOOL_SIZE=5 before the process starts, all five can run at once. This needs enough CPU cores.