Hello guys, so today’s post would be centered on Node.js and how you can use it to run processor-intensive tasks.
So due to nodes single-threaded none blocking feature, it won’t be advised to carry out intensive tasks (processor churning tasks on node.js). So what do I mean by intensive here? I mean stuff like calculating the Fibonacci sequence of a number or some intensive calculation like summing up numbers from 1 to a billion. In some cases, you can use closures as a caching technique. But I won’t talk about that in this post.
Node.js has a feature that allows you to run intensive tasks in more than one process. And the module that enables you to have access to this feature is the “child process”
What does a process mean? A process is an execution instance of a program.
So to test how good node is in running CPU intensive tasks I wrote a code that sums consecutive integers from various numbers to a billion.
I also had to create two client codes that:-
- Makes a call to the server to use the normal single-threaded feature
- Makes a call to the server to spawn multiple processes instead of queuing the task
I used the http2 module since it allowed me to create a mock server and client. Also do note that the http2 module isn’t compatible with the http module. The http2 module also emits almost the same event on both the client and server-side.
So below is the task that I was using to test the two instances
This is my server code
This is the code to forked process.
Below is the code to my two clients:-
There might be better ways of obtaining the bench-mark between using the single thread or creating multiple processes to handle the task but from my test creating multiple processes was exponentially better when using a single thread.
How the client code functions:
The two client code files are almost the same. The difference they have is that they make a request to different routes ( 2 routes to be specific).
The “norm” route uses the single thread and the “calculate” route creates multiple processes to handle the task.
Three hundred connections are made in both of the client codes to the server. The val parameter sends the starting value of the loop.
So the first connection finds the total from 0 to a billion, the second obtains the total from 1 to a billion and it keeps going until 299 to a billion.
So this is what I obtained from the test:-
After creating three hundred connections in two of the clients I got this result:-
The normal.js client took 490.9 seconds to complete
The multi.js client took 141.83 seconds to complete
From the analysis above you can see that creating multiple processes to work on the task is exponentially faster than using just a single thread.
The time to complete the task would probably vary on your system but you would notice that using multiple processes would be faster.
Here is a repo to the code used https://github.com/sammychinedu2ky/nodejs-child-process. To try out the test, ensure that server.js is running. Then you can pick any of the clients( multi.js or normal.js) and try out the test.
Also, leave a comment if you have a question or have something to contribute.
To learn more about creating multiple processes, try and check out this link. And to learn more about the http2 module check out this link