It's often said that Node.js app is single-threaded, however there're total 3 threads resulted from the commands below. What are they?
$ node -e 'while (true) {}' &
=> <node_pid>
$ ps huH p <node_pid> | wc -l
=> 3
The application JavaScript code you write is single-threaded as Node uses callbacks to deal with blocking IO, which are then processed in order by a single event loop. However, all of this is executed by the underlying platform written in C++, the V8 engine, as well as the libuv library written in C. These two components do not share the constraints of the event loop, and are able to spawn multiple threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With