• We just launched and are currently in beta. Join us as we build and grow the community.

Node.js is not single-threaded

brenbear87

Responsive UI Specialist
Divine
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
125
Likes
55
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.
  • 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()
b. All zlib APIs other than those using libuv thread pool synchronously.
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.
 

435,797

314,092

314,101

Top