DEV Community

Discussion on: Everything you need to know about Node.js

Collapse
 
jorge_rockr profile image
Jorge Ramón

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.

Collapse
 
karataev profile image
Eugene Karataev

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.

Thread Thread
 
jorge_rockr profile image
Jorge Ramón

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.