I was doing some experiments with Node.js
even though we say Node.js is single threaded because the event loop itself runs on a single thread, some parts of the code actually run concurrently.
By default, Node.js has a thread pool with 4 threads. That is why when you run this code, the first 1 to 4 tasks run at almost the same time.
The 5th task has to wait until one of those 4 threads finishes.
run the code and see it yourself
const crypto = require('crypto');
const start = Date.now();
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('1:', Date.now() - start);
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('2:', Date.now() - start);
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('3:', Date.now() - start);
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('4:', Date.now() - start);
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('5:', Date.now() - start);
});