# Node.js Worker Threads Demo
This project demonstrates how to handle **CPU-bound tasks** in Node.js using **Worker Threads**.
It includes multiple API routes to test **blocking**, **single-worker**, and **multi-worker** scenarios, with simple timing comparisons.
## Features
- `/non-blocking` – Regular API route without CPU-heavy computation
- `/blocking` – CPU-heavy computation using:
- **Single Worker**
- **Four Workers** (task distributed across threads)
- Easy to compare **response times** and **CPU usage**
- Demonstrates how to prevent blocking the Node.js main thread
## API Endpoints
### 1. `/non-blocking`
- **Method:** `GET`
- **Description:** Simple non-blocking route that returns a message immediately.
- **Example:**
```bash
curl http://localhost:4000/non-blocking- Response:
this is Non Blocking Response Sent
- Method:
GET - Description: Performs a CPU-intensive computation.
- Behavior:
- Single Worker: Offloads the entire computation to one worker thread.
- Four Workers: Splits the task evenly across four worker threads.
- Example (with timing on Windows):
curl -o NUL -s -w "Total time: %{time_total}s\n" http://localhost:4000/blocking - Cross-platform timing example:
curl -s -w "Total time: %{time_total}s\n" -o /dev/null http://localhost:4000/blocking
Observe the timing differences between single worker and four workers.
Compare with/non-blockingto see main thread responsiveness.
- Single Worker: The entire computation runs in one Worker Thread.
- Four Workers: The computation is divided into four equal parts, executed in parallel, and results are combined.
- Workers receive data via
workerDataand communicate results withpostMessage. - The main thread uses
Promise.allto aggregate results from multiple workers.
- The number of workers should ideally match the number of logical CPU cores.
- Using too many workers can increase memory usage and reduce performance due to context switching.
- Worker Threads are beneficial only for CPU-bound tasks. For I/O-bound tasks, Node.js's event loop is already efficient.
Use curl, Postman, or any HTTP client to measure response times.
| Route | Workers | Approximate Response Time |
|---|---|---|
/non-blocking |
None | ~0.01s |
/blocking |
1 worker | ~7–8s |
/blocking |
4 workers | ~2–3s |
curl -s -w "Total time: %{time_total}s\n" -o /dev/null http://localhost:4000/non-blocking
curl -s -w "Total time: %{time_total}s\n" -o /dev/null http://localhost:4000/blockingTimes will vary depending on your CPU and system load.
├── index.js # Express server and route definitions
├── worker.js # Single worker implementation
├── four_worker.js # Four-worker task splitting implementation
├── package.json
└── README.md
This project helps understand:
- Limitations of the Node.js main thread with CPU-bound tasks
- How to use Worker Threads for offloading work
- Benefits of task distribution across multiple workers
- Practical performance testing with multithreading
Author: Raihanul Islam Sharif
Goal: Explore multithreading in Node.js and optimize CPU-bound operations