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

 

The Cluster module in NodeJS has what you need. The documentation has a simple example to start. The way it works is that is causes the threads, (which run on as each individual CPU core) to share the same port. It then load balances requests on the various threads.

If you want to have a long running compute operation, you might try the NodeJS Child Process core module.

 

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 Jan 12

Kotlin: How Can a Programming Language be so Young And so Mature at The Same Time?

In this article, we’re going to explore why young programming languages with modern features can’t be adopted quickly. Additionally, we’re going to take a look at one exceptional example that got specific parameters right to be both young, modern and mature, just ready for adoption at small and big scale.

kinensake profile image