re: Everything you need to know about Node.js VIEW POST

re: Thanks for the great post! Let's say I need a worker to do heavy CPU calculations. How does the worker works under the hood? Does it start on a se...

Thanks for commenting.

Node.js by default works in a single CPU core, so worker thread will spawn and execute in the same CPU core that the main thread does.

If you want to deploy a Node.js application across all CPU cores you need to write some code using cluster native module. Thanksfully there is a library called PM2 which does the dirty work for you and deploys a Node.js application in all CPU cores with its built in load balancer.

The worker thread real problem is spawn like crazy, since creating a subthread (worker thread) isn't cheap (talking about CPU time and resources) but it's cheaper than fork the same process.


Well, if a separate worker and main thread are executed in one CPU core, then they share the same resource.
Is it correct that if we write heavy CPU code in "chunks", i.e. we will return execution context from a heavy function to the function which handles http requests with some interval, then we will emulate workers? But in this case it's not necessary for Node to allocate resources for a subthread, i.e. it's cheaper in terms of performance.

Remember Worker as OS thread actually so I don't think they will be emulated in a single CPU core case. Anyway that's an interesting question but unfortunately workers are pretty "new" so there isn't enough information about them.

Code of Conduct Report abuse