markdown guide

In most web server technologies, each request takes up a thread. Each thread occupies approximately 1 MB of RAM, and they take long to get started or get recycled. Also, some of these technologies freeze the current thread when looking up stuff in a database till they get an answer. So, if you have 10 000 requests at the same time, you need 10 GB of RAM.

NodeJS, instead, deals with all code running time in a single thread. So if your requests are brief impulses requiring little CPU crunching, it can answer them much more faster than the multi-thread model. That is why you shouldn't use Node for data heavy stuff, but for projects that require reading and writing data, since they won't hang your only thread and won't occupy much CPU time.

Regarding the part where you talk about the 4 threads Node has: you can simulate multi-threading in NodeJS by having 4 instances of the same NodeJS project running, similar to like having 4 browser tabs of the same website running. That's a common trick to have NodeJS working in all available CPU cores.

Hope it was helpful


Node.js doesn't have 4 threads, as JavaScript is single-threaded. Node.js' "asynchronous"-ness is a well decorated lie because while you can use async/await in the code, you're only doing that to free the single thread Node.js has to do other things it has planned.

In short, every "async" call, or calls through setTimeout or setImmediate adds the function to a stack of different functions to call. Node.js then go through them in order.

The only right answer to "how to make Node.js fast" is to have several Node.js processes running the same code, and have a way to distribute requests to them.


The answer is event loop

If your backend processes are heavy on IO then you will be able effectively to serve only 4 clients at a time, but otherwise you will be able to serve more.

at the same time quickly

It doesn't do it at the same time (at least not more that 4 items at the same time), it does it in turns.

Classic DEV Post from Dec 31 '18

Who's looking for open source contributors? (Dec 31st edition)

Find something to work on or promote your project here. Please shamelessly pro...

kinensake profile image