Skip to content

Instantly share code, notes, and snippets.

@rjoydip-zz
Last active January 5, 2024 13:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rjoydip-zz/36031b3b6d46a9d638e3a97816d2a137 to your computer and use it in GitHub Desktop.
Save rjoydip-zz/36031b3b6d46a9d638e3a97816d2a137 to your computer and use it in GitHub Desktop.
How you can set `UV_THREADPOOL_SIZE` value?
const {readdir} = require('fs');
process.env.UV_THREADPOOL_SIZE = 6; // This will work
readdir('.', () => {
process.env.UV_THREADPOOL_SIZE = 20; // This won't
});
[1,2,3].forEach(element => {
process.env.UV_THREADPOOL_SIZE = 7; // This will work because this isn't a async task
});
process.stdout.write("[UV_THREADPOOL_SIZE]", process.env.UV_THREADPOOL_SIZE);
// Note: You cannot change the size of the thread pool once it is created or entered in the event-loop/worker-thread.
// npm-script: cross-env UV_THREADPOOL_SIZE=5 node index
// install "cross-env" as devDependencies

When you need to set value to "UV_THREADPOOL_SIZE"?

  • Libuv has a default thread pool size of 4, and uses a queue to manage access to the thread pool - the upshot is that if you have 5 long-running DB queries all going at the same time, one of them (and any other asynchronous action that relies on the thread pool) will be waiting for those queries to finish before they even get started.

  • Note, however, that tuning UV_THREADPOOL_SIZE may make more sense for a standalone application like a CLI written in Node.js. If you are standing up a bunch of Node.js processes using the cluster module then I would be surprised if tuning UV_THREADPOOL_SIZE was particularly beneficial for you. But if your application resembles the web tooling benchmarks then tuning UV_THREADPOOL_SIZE may help with performance.

@tapas77
Copy link

tapas77 commented May 2, 2021

this method is not working in windows.As it always gives same output for every threadpool size

@tapas77
Copy link

tapas77 commented May 2, 2021

const fs = require('fs');
const crypto = require('crypto');

const start = Date.now();
process.env.UV_THREADPOOL_SIZE = 1;

setTimeout(()=>console.log("Timer 1 finished"),0);
setImmediate(()=>console.log("Immediate 1 finished"));

fs.readFile("./test-file.txt", () => {
console.log("I/O finished");
console.log("---------------------------------------------");

setTimeout(()=>console.log("Timer 2 finished"), 0);
setTimeout(()=>console.log("Timer 3 finished"), 3000);
setImmediate(()=>console.log("Immediate 2 finished"));

process.nextTick(() => console.log("process nextTick finished"));
crypto.pbkdf2("password", "salt", 100000, 1024, "sha512", ()=>{
    console.log(Date.now()-start, "Password encrypted");
});
crypto.pbkdf2("password", "salt", 100000, 1024, "sha512", ()=>{
    console.log(Date.now()-start, "Password encrypted");
});
crypto.pbkdf2("password", "salt", 100000, 1024, "sha512", ()=>{
    console.log(Date.now()-start, "Password encrypted");
});
crypto.pbkdf2("password", "salt", 100000, 1024, "sha512", ()=>{
    console.log(Date.now()-start, "Password encrypted");
});

});

console.log("Hello from the top level code");

here is my code on the top.It is not working in windows but it works on mac. Please help me to run it on windows.

@rjoydip-zz
Copy link
Author

@tapas77 Your example is working on my windows system.

@tapas77
Copy link

tapas77 commented May 3, 2021

If you change the THREADPOOL_SIZE to 2, what is the output?

@tapas77
Copy link

tapas77 commented May 3, 2021 via email

@rjoydip-zz
Copy link
Author

You will get same output that doesn't change your application output. To changing UV_THREADPOOL_SIZE values means you resizing thread pool queue. That means you can execute that many numbers (e.g.: If value is 6 then you have 7 queues) of async tasks at the same time.

@RepentAndBelieveTheGospel

The code tapas77 posted is from a Node.js course I am also taking (Master Node by building a real-world RESTful API and web app (with authentication, Node.js security, payments & more) by Jonas Schmedtmann). The instructor is running the server on a Mac, which makes it work as is. I've managed to make it work on Windows by installing cross-env (I did it globally), which rjoydip mentioned at the bottom. Setting the variable through cross-env works and you get the expected delay in the console, due to only one thread pool being available.

@tapas77
Copy link

tapas77 commented Jun 28, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment