Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why node.js is fast when it's single threaded?

Despite being single threaded, how is node.js is faster? I haven't run any tests to find statistics, but while digging in the node.js forums, I find everyone says it's faster and more lightweight. But no matter how light weight it is, how can a single threaded server be faster than multi thread ones?

like image 711
Paul Shan Avatar asked Sep 06 '25 23:09

Paul Shan


1 Answers

First, why is a program faster when multi-threaded ?

It's partly due to the fact a multi-threaded program can run on multiple cores but the main reason, by far, is that when a thread is waiting for some IO operation (which is very often, especially in a server), the other threads can still progress.

Now, what about node ?

Node isn't single threaded. The user script in JS is executed in one thread but all IO operations are natively handled by libuv and the OS which are multi-threaded.

More explanation here.

In practice, this means that several requests are handled in parallel. Here's a very (very) simplified example of a possible sequence of actions:

user script                     | node + OS "threads" (libuv)
-------------------------------------------------------------
receive and analyze request 1   |
ask node for file 1             | fetching file 1
receive and analyze request 2   | fetching file 1
ask node for file 2             | fetching file 1, fetching file 2
prepare response header 1       | fetching file 2
tell node to send file 1        | send file 1, fetching file 2
prepare response header 2       | send file 1
tell node to send file 2        | send file 1, send file 2

The whole architecture of node (and io.js) makes it simple to have a high level of parallelism. The user thread is only called by the event loop for very short tasks which stop at the next IO operation (well, not really only IO, but most often) when your code gives to node a callback that will be called when the operation finished.

Of course this only works when you're using the asynchronous functions of Node. Any time you use a function ending in "Sync" like writeFileSync, you're defeating the concurrency.

like image 67
Denys Séguret Avatar answered Sep 08 '25 13:09

Denys Séguret