Harness the power of multi-threading in Node.js to improve the performance of your REST APIs. This repository demonstrates how to use the worker_threads module to handle computationally intensive tasks without blocking the main thread.
- Blocking API Example: Demonstrates how a heavy computation can block the main thread.
- Non-Blocking API Example: Utilizes
worker_threadsto offload tasks and ensure the main thread remains responsive. - Performance Impact:
- If you hit the Blocking API first, subsequent requests (e.g., a listing API) will experience delays.
- Using the Non-Blocking API, the same listing API responds faster, demonstrating significant improvements, especially under high traffic or large database operations.
You’ll need Node.js installed on your system.
-
For Windows:
Download the installer from the official Node.js website and follow the setup instructions. Ensuregitis available in your PATH (Download Git). -
For Ubuntu:
Use the following commands to install Node.js and npm:sudo apt install nodejs sudo apt install npm
-
For Other Operating Systems:
Visit the official Node.js website and follow the installation instructions specific to your OS.
Verify the installation:
node --version
# Example Output: v16.11.3
npm --version
# Example Output: 8.1.0Update npm (if needed):
npm install -g npmInstall Yarn globally using npm:
npm install -g yarnVerify the installation:
yarn --versionClone the repository and install dependencies:
git clone https://github.com/sanjaydeveloper15/node-js-worker-threads
cd node-js-worker-threads
yarn installStart the development server:
yarn startGenerate a production build:
yarn build-
Blocking API:
- Simulates a computationally heavy task on the main thread.
- Affects the responsiveness of other APIs during execution.
-
Non-Blocking API:
- Offloads heavy tasks to a worker thread using the
worker_threadsmodule. - Keeps the main thread responsive for other API requests.
- Offloads heavy tasks to a worker thread using the
Usage Tip:
Compare the response times of the Listing API when used with the Blocking and Non-Blocking APIs to observe the performance improvements.
Node.js is single-threaded by default, which means computationally intensive tasks can block the event loop and degrade performance. The worker_threads module provides a way to run JavaScript code in parallel threads, enabling:
- Improved Performance for CPU-intensive tasks.
- Responsiveness: Keeps the main thread free for handling I/O operations.
- Node.js Worker Threads Documentation
- Visual Studio Code: The recommended IDE for this project.
- Education Funda (YouTube): Tutorials and educational content.
- Sanjay Kumar (Portfolio): Learn more about the developer.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page or submit a pull request.
Developed by Sanjay Kumar.
- Portfolio: https://sanjaydeveloper.netlify.app
- Twitter: @sanjaykumarwebs
Run this project, test the APIs, and see how offloading tasks to worker threads can drastically enhance your application's performance. 🚀
