I am trying to understand the threadpool in nodeJS.
on running the code with creating process.env.UV_THREADPOOL_SIZE = 5;
process.env.UV_THREADPOOL_SIZE = 5;
const https = require('https');
const crypto = require('crypto');
const fs = require('fs');
const start = Date.now()
function doRequest() {
https.request('https://google.com', res => {
res.on('data', () => {});
res.on('end', () => {
console.log('Request:', Date.now() - start)
})
})
.end()
}
function doHash(){
crypto.pbkdf2("a", "b", 100000, 512, 'sha512', () => {
console.log("Hash:", Date.now() - start);
})
}
doRequest();
fs.readFile('multitask.js', 'utf8', () => {
console.log('fs:', Date.now() - start)
});
doHash();
doHash();
doHash();
doHash();
I get the output in the terminal:
$ node multitask.js
Request: 641
Hash: 4922
fs: 4925
Hash: 5014
Hash: 5039
Hash: 6512
And after changing the threadpool size to 1: I get the same output.
Request: 501
Hash: 4025
fs: 4028
Hash: 4087
Hash: 4156
Hash: 5079
Could anyone tell me where is the problem exactly?
This thread pool is internally used to run all file system operations, as well as getaddrinfo and getnameinfo requests. Its default size is 4, but it can be changed at startup time by setting the UV_THREADPOOL_SIZE environment variable to any value (the absolute maximum is 1024). Changed in version 1.30.
To increase the threadpool size, set the environment variable UV_THREADPOOL_SIZE=N up to 1024.
The easiest solution for me was just to add a npm script entry like so:
{
...
"main": "app.js",
"scripts": {
"start": "set UV_THREADPOOL_SIZE=2 & node app.js"
},
...
}
And then, in the cmd:
npm run start
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With