OP 01 June, 2024 - 04:43 AM
(This post was last modified: 01 June, 2024 - 04:44 AM by itsmohit12. Edited 1 time in total.)
Node.js itself is not single-threaded.
Node.js developers often confuse the main single-threaded event loop with Node.js entirely.
When a Node.js app is running, it automatically creates 4 threads under the worker pool for blocking tasks.
So at any given time, there are at least five threads.
This worker pool is managed by Libuv.
The blocking tasks are mainly I/O-bound and CPU-intensive.
The main thread/event loop works for JavaScript execution as usual, but the worker pool takes care of blocking tasks for the main loop.
So, Node.js shouldn't be confused.
Thanks for reading.
Node.js developers often confuse the main single-threaded event loop with Node.js entirely.
When a Node.js app is running, it automatically creates 4 threads under the worker pool for blocking tasks.
So at any given time, there are at least five threads.
This worker pool is managed by Libuv.
The blocking tasks are mainly I/O-bound and CPU-intensive.
- I/O bound
a. DNS: dns.lookup(), dns.lookupService()
b. File system except fs.FSWatcher()
- CPU-intensive tasks
a. Some crypto methods such as crypto.pbkdf2(), crypto.scrypt(), crypto.randomBytes(), crypto.randomFill(), crypto.generateKeyPair()
The main thread/event loop works for JavaScript execution as usual, but the worker pool takes care of blocking tasks for the main loop.
So, Node.js shouldn't be confused.
Thanks for reading.