An open API service indexing awesome lists of open source software.

https://github.com/rupachowrasia/nodejs-multithreading-worker-threads

A sample app to demonstarte multithreading in node.js using worker-threads
https://github.com/rupachowrasia/nodejs-multithreading-worker-threads

expressjs multithreading nodejs offload-tasks shared-memory threads worker-threads

Last synced: about 1 month ago
JSON representation

A sample app to demonstarte multithreading in node.js using worker-threads

Awesome Lists containing this project

README

        

# Multithreading in node.js with Worker Thread
> Node.js is single-threaded by default (everything runs on the main thread), which is great for I/O tasks but bad for heavy computation (e.g., image processing, encryption, parsing large files). To offload heavy computational task we use
Worker Threads in node.js.

## πŸš€ Features

- A Worker Thread is a way to run JavaScript in parallel on multiple threads (useful for CPU-heavy tasks).
- They share memory, but don’t spawn new processes.
- Ideal for CPU-bound tasks (math, compression, etc.) β€” not I/O-heavy work.
- API:
- Run check: isMainThread
- Send/Receive: parentPort.postMessage(), .on()
- Input data: workerData
- File path: Use new URL('./worker.js', import.meta.url) in ESM

## πŸ›  Tech Stack

- Node.js
- Express
- Node.js worker-thread module

## πŸ“¦ Installation

```bash
# Clone the repo
git clone https://github.com/rupachowrasia/nodejs-multithreading-worker-threads.git

# Move into the project directory
cd nodejs-multithreading-worker-threads

# Install dependencies
npm install

# Run the app
npm run start