I opened my activity monitory in OSX to to see how Node was getting along and to my surprise it's using 8 threads. How can in be!!?
Node. js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node. js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.
Node JS Platform does not follow Request/Response Multi-Threaded Stateless Model. It follows Single Threaded with Event Loop Model. Node JS Processing model mainly based on Javascript Event based model with Javascript callback mechanism.
Node. js uses two kinds of threads: a main thread handled by the event loop and several auxiliary threads in the worker pool. The event loop is the mechanism that takes callbacks (functions) and registers them to be executed at some point in the future. It operates in the same thread as the proper JavaScript code.
Node. js is a single-threaded language and uses the multiple threads in the background for certain tasks as I/O calls but it does not expose child threads to the developer. But node. js gives us ways to work around if we really need to do some work parallelly to our main single thread process.
Node.js is single-threaded.
However, it is built on libuv which handles all low-level platform-dependent stuff, including async IO.
Now the issue is that there is no good API for async IO in operating systems nowadays. Different APIs exist, but they all have their issues.
So in order to implement cross-platform async API, libuv emulates it using a thread pool. This is where those threads are coming from.
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