Node.js worker_threads
vs child_process.fork
performance
#44264
Replies: 7 comments 10 replies
-
If your threads are exhausting their memory, I can imagine forking a separate process (which would have more memory) could be much faster. |
Beta Was this translation helpful? Give feedback.
-
the difference between worker and child process in this context are:
so the latency difference will be a function of how long the task is run, and how much you interact with the main (parent) process. In the absence of any continued interaction, it is a sole function of the computation. Longer the task, the child process will prove to be running faster. |
Beta Was this translation helpful? Give feedback.
-
I am not sure of the internal implementation of |
Beta Was this translation helpful? Give feedback.
-
Without having looked at the code/benchmark, have you tried using a worker pool, for example https://github.com/piscinajs/piscina ? |
Beta Was this translation helpful? Give feedback.
-
Out of curiosity, I put together a demo with Deno and to my surprise saw results on par with Definitely curious what they are doing differently and if there are opportunities to improve |
Beta Was this translation helpful? Give feedback.
-
one of the thing that we can check is where the CPU is actually spent (or not spent), through the perf tool This may give us some insights.
|
Beta Was this translation helpful? Give feedback.
-
@nodejs/performance is this actionable? |
Beta Was this translation helpful? Give feedback.
-
Hello 👋
I'm in the process of prototyping a module for running code transformations and gathering metrics over a large number of text files (anywhere from 5,000 to 50,000). My implementation is heavily inspired by jscodeshift, however, instead of using
node:child_process.fork
I opt’d fornode:worker_threads
. When comparing the speed differences between my module andjscodeshift
, I foundjscodeshift
was noticeably faster. After making a few adjustments (such as: batching and sending 50 files at a time to each free worker), I came to the (tentative) conclusion the performance differences are in regards tonode:child_process.fork
vsnode:worker_threads
.In an effort to validate this assumption, I created a repository that benchmarks the performance differences between
node:child_process.fork
andnode:worker_threads
(scoped to my particular application). That said, I haven’t done much benchmarking in my career and am very open to feedback on how to increase the accuracy of the results.Speaking of results. My current benchmarks are consistently showing
node:child_process.fork
is processing files faster than mynode:worker_threads
implementation.I would greatly appreciate any insight as too why this is or what adjustments I can make to my
node:worker_threads
implementation!https://github.com/aaronccasanova/node-parallel-benchmarks
Beta Was this translation helpful? Give feedback.
All reactions