Scenarios:
Before we dive into worker threads, let's consider some scenarios...
Suppose a client uploads a large file to a server that needs to be modified or involves processing thousands of data points in the background. If the server waits for this task to finish, the client is left waiting and unable to explore other features. Imagine if a client had to wait for 5 minutes without being able to do anything else—this would be frustrating and far from user-friendly!
Consider another situation where you upload a profile image, and it takes a long time to process, convert, and store in the database. During this time, if the server prevents you from performing other tasks, it significantly reduces the user experience.
In the first case, wouldn't it be better if the server allowed you to explore other features while the file is still being processed? This way, you wouldn't have to wait (since the server wouldn't block you), resulting in a smoother experience.
In the second case, what if the image processing happens in the background, allowing you to continue using other features without waiting?
Solution:
So, what's an effective way to optimize the system's performance in these scenarios? While there are several approaches, using worker threads is a great solution. Worker threads were introduced in Node.js version 10 and are especially useful for performing CPU-intensive tasks in parallel, reducing the load on the main CPU.
Worker threads operate in the background, creating a separate thread that handles intensive computations without blocking the main thread, thus allowing the server to remain responsive for other tasks. While JavaScript is traditionally a single-threaded language and Node.js operates in a single-threaded environment, worker threads enable multithreading by distributing operations across multiple threads. This parallel execution optimizes resource usage and significantly reduces processing time.
Implementation of worker_thread:
Today we will implement a simple nodejs application with default package worker_threads . First create an express server where a simple get request executes.
First initialize the project:
$ npm init -y
Install express module and nodemon:
$ npm i express nodemon
Creating a simple nodejs server which runs on port 3000.
Import express from ‘express’; const app = express(); const port = 3000; // Basic endpoint to test server app.get(‘/’, (req, res) => { res.send(‘Hello World!’); }); app.listen(port, () => console.log(`Server running on port ${port}`));
Here we created a server which will run on port 3000.
To run, let's modify our package.json file.
Add type as module as below to get the ES6 modules. Also modify under the scripts portion as below.
{ "name": "worker_express", "version": "1.0.0", "description": "", "main": "index.js", "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js", "dev": "nodemon index.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "dotenv": "^16.4.5", "express": "^4.19.2", "nodemon": "^3.1.4" } }
Now let’s run our application as dev mode with nodemon:
$ npm run dev
You will see the message Server running on port 3000. Now go to the localhost:3000 and you can see the Hello World! As far we did just a simple nodejs express server.
Now lets’ create another file named service.js
Here we can create a fibonacci sequence function that finds the nth number fibonacci sequence.
// service.js function fibonacci(n) { if (n <p>Now let’s add another api endpoint to the index.js file and call the fibonacci function from the service.js file. We will calculate the 40th Fibonacci number as an example.<br> </p> <pre class="brush:php;toolbar:false">import fibonacci from "./service.js"; // Fibonacci endpoint app.get('/fibonacci', (req, res) => { fibonacci(40) res.send('fibonacci called'); })
If you hit the URL http://localhost:3000/fibonacci, you will see that it delays a bit, making you wait. The delay time depends on the computation.
You can try again by commenting the function and see it takes less time which is about millisecond.
In this case you might have to do other heavy operations which are time consuming and reduce performance.
In this case, we can use the worker_threads module, which has been available by default in Node.js since version 10. Now let’s modify the code to apply worker_threads and see the effect.
Import Worker from worker_thread which is node js default package.
import { Worker } from "worker_threads";
Now modify the api endpoint like below.
// Endpoint using worker thread for CPU-intensive task app.get('/fibonacci', (req, res) => { const worker = new Worker('./service.js', {workerData: 40}); // Handle messages from worker thread worker.on('message', (resolve) => console.log(resolve)); res.send('fibonacci called'); })
Here, we create a worker instance and set the file name service.js as the first argument, while the second argument passes parameters through workerData. You can change the workerData parameter to any other data instead of 40.
worker.on(‘message’, ….) This sets up an event listener on the worker for the ‘message’ event. The message event is emitted by the worker when it sends data back to the main thread using parentrPort.postMessage(...).
(resolve) => console.log(resolve) this is a callback function that will be executed when the worker sends back the data after operation. The received message(data) is passed to this function as the resolve parameter.
Now let’s update our service.js file.
import { workerData, parentPort } from 'worker_threads'; // Function to compute Fibonacci sequence function fibonacci(n) { if (n <p>Here, we import workerData and parentPort, which allow us to receive the data sent through workerData and return the result via the postMessage method of parentPort, both imported from worker_threads.</p> <p><strong>Test the Setup</strong>:<br> Now, send a request to http://localhost:3000/fibonacci and notice that the server no longer blocks the main thread. The time-consuming operation occurs in the background on a separate thread, significantly reducing the response time and improving user experience.</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172501398654385.png?x-oss-process=image/resize,p_40" class="lazy" alt="Enhance Node.js Server Performance with Worker Threads"></p> <p>Here is the source code in github.</p>
The above is the detailed content of Enhance Node.js Server Performance with Worker Threads. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
