3 ms·
The CPU can only do one thing at a time even with threads. It just hides that fact from you better. In an app that grabs a page from a database and returns it,
by vertex-four 12y ago
The CPU can only do one thing at a time even with threads. It just hides that fact from you better.
In an app that grabs a page from a database and returns it, the node.js app will receive a request from a user, make a request to the database, receive a request from another user, make a request to the database, then wait for either database request to return, then return it to the user, then when the other returns, return that to the user.
Your program doesn't deal with one whole request at a time, it chops it up into lots of little "wait for this event" parts and node.js has an event loop that listens for events, which can happen in an arbitrary order. So you can be waiting for hundreds of events to happen for hundreds of requests, and as node.js doesn't make you wait for the first one to process the second, nobody's holding up anyone else.
Threading does nearly exactly this automatically... except that you need memory per thread in order to maintain the illusion that each thread has complete control over the CPU (its own stack, etc), and it can also split up CPU-bound workloads automatically. node.js needs much less memory per event it's waiting for.