Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple threads vs fork process in NodeJS

Tags:

node.js

I'm trying to find a better way to run some cpu bound tasks using NodeJS. There are 2 options as I know, fork processes and worker threads. So I have ran a benchmark between them and found out that fork process is faster.

So, I have some questions here:

  1. Why is the fork process method faster?
  2. Are the worker threads useless? Or what are the worker threads advantages, consumes less cpu/memory?

I'm running the code on MacOS(2.2 GHz Intel Core i7, 16 GB 1600 MHz DDR3) using node v10.15.3

the benchmark code is in my gist:

threads pool library microjob and process pool library node-worker-farm are used in my code as you can see. The running cmd is : node --experimental-worker benchmark.js

like image 988
Liu Wenzhe Avatar asked May 17 '19 06:05

Liu Wenzhe


People also ask

Can NodeJS run multiple threads?

Node. js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node. js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.

What is multi threading in NodeJS?

So, What Is Multithreading in Node. js? Multithreading is a program execution model that allows multiple threads to be created within a process. The threads execute independently but concurrently share process resources.

What is the difference between forking a process and spawning a thread?

Threads are functions run in parallel, fork is a new process with parents inheritance. Threads are good to execute a task in parallel, while forks are independent process, that also are running simultaneously.


1 Answers

I think @gireeshpunathil's comment from https://github.com/nodejs/help/issues/1920 can help a lot:

there is no single formulae that computes which method will be faster. It depends on a number of parameters:

  • the running time of the job itself
  • the interaction of the job with the spawning code
  • the machine characteristics, specifically cpu, memory and scheduling

the fork uses native fork and exec, that is heavy-weight than thread creation. fork creates a new node instance with new v8, isolate, libuv etc. worker does not create new node instance. forked process uses IPC channel to talk to the forking process, if needed. workers use in-memory buffers.

So:

  • if the execution time of the microjob is too small compared to the process creation, worker method will be faster.
  • if the job communicates with the driver, the frequency of communication and the cost of communication will decide the winner
  • if the job runs as long independent piece of code with no interaction, fork method will eventually prove faster (I think that
    is happening here)
like image 161
Liu Wenzhe Avatar answered Nov 16 '22 00:11

Liu Wenzhe