Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

sanjaydeveloper15/node-js-worker-threads

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛠️ Node.js Worker Threads

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.

Node Worker Threads - Education Funda


🚀 Features

  • Blocking API Example: Demonstrates how a heavy computation can block the main thread.
  • Non-Blocking API Example: Utilizes worker_threads to 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.

🔧 Requirements

Node.js

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. Ensure git is 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.0

Update npm (if needed):

npm install -g npm

Yarn

Install Yarn globally using npm:

npm install -g yarn

Verify the installation:

yarn --version

📦 Installation

Clone the repository and install dependencies:

git clone https://github.com/sanjaydeveloper15/node-js-worker-threads
cd node-js-worker-threads
yarn install

🚀 Running the Project

Start the development server:

yarn start

🏗️ Build for Production

Generate a production build:

yarn build

📋 Example APIs

  1. Blocking API:

    • Simulates a computationally heavy task on the main thread.
    • Affects the responsiveness of other APIs during execution.
  2. Non-Blocking API:

    • Offloads heavy tasks to a worker thread using the worker_threads module.
    • Keeps the main thread responsive for other API requests.

Usage Tip:
Compare the response times of the Listing API when used with the Blocking and Non-Blocking APIs to observe the performance improvements.


🌐 Why Use Worker Threads?

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.

💡 Recommended Tools and References


📄 License

This project is licensed under the MIT License. See the LICENSE file for details.


🤝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page or submit a pull request.


👨‍💻 Author

Developed by Sanjay Kumar.


✨ Showcase the Power of Worker Threads!

Run this project, test the APIs, and see how offloading tasks to worker threads can drastically enhance your application's performance. 🚀

About

Demonstrating multi-threading in Node.js using the node:worker_threads module, enabling parallel execution for computationally intensive tasks and improved performance.

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.