12

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?

ventaquil
  • 2,780
  • 3
  • 23
  • 48
cptiwari20
  • 442
  • 1
  • 7
  • 18

3 Answers3

17

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
Shahar G.
  • 1,440
  • 12
  • 22
16

On linux your code works fine:

UV_THREADPOOL_SIZE = 1;

fs: 20
Request: 108
Hash: 817
Hash: 1621
Hash: 2399
Hash: 3175

UV_THREADPOOL_SIZE = 5

fs: 11
Request: 120
Hash: 836
Hash: 857
Hash: 859
Hash: 871

If you're using windows, instead of setting it inside your javascript file, you have to set it before calling the script.

set UV_THREADPOOL_SIZE=1 & node app.js
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
0

The problem we have here is coming from the udemy course Node JS Advanced Concepts. I tried this to set the threads dynamically. But on windows, it won't work. Follow the above instructions on Windows.

const OS = require("os")
process.env.UV_THREADPOOL_SIZE = OS.cpus().length