Node.js Course
Node.js
/
Advanced

Thread Pool

Definition

A collection of background threads maintained by `libuv` to execute heavy tasks that cannot be handled asynchronously by the OS kernel, preventing them from blocking the main Event Loop.

Explain Like I'm New

Wait, I thought Node was single-threaded? The JavaScript code YOU write is single-threaded. But deep in the basement, `libuv` maintains a secret team of 4 C++ workers (The Thread Pool). When you ask Node to hash a password or read a file, it secretly passes that work to the basement workers. They do the heavy lifting simultaneously while your main thread keeps serving web pages.

Real World Example

Cryptographic functions (`pbkdf2`), File System operations (`fs.readFile`), and DNS lookups all use the Thread Pool automatically.

Common Use Cases

  • •Optimizing CPU-bound internal operations

Interactive Example

Loading Code Playground...

Interview Questions

basic

  • How many threads does the libuv Thread Pool have by default?

intermediate

  • What happens if you run 5 heavy cryptography functions at the exact same time?

Flash Cards

Question

How many threads by default?

Click to reveal answer
Answer

Four (4).

Question

What happens with 5 tasks?

Click to reveal answer
Answer

The first 4 tasks will immediately occupy the 4 threads in the pool. The 5th task must wait in line. It will not start executing until one of the first 4 tasks finishes and frees up a thread.