Search code examples
javascriptnode.jslibuv

process.env.UV_THREADPOOL_SIZE not working?


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?


Solution

  • 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