Quickest way to call a computationally expensive function many times in Node.js
NickName:dislick Ask DateTime:2020-04-16T20:27:42

Quickest way to call a computationally expensive function many times in Node.js

I have a function that is somewhat computationally expensive, we are talking about 2–5ms here.

function heavyWork() {
  // ...
}

heavyWork() gets called a couple hundert times by different parts of my code at once which blocks Node.js' event loop. I was thinking about moving it to a child process by using child_process.fork, however even just sending ~500 IPC messages takes about the same time as computing it in the main process. Theoretically I could live with this solution, it's just a bit slower than before and does not block the event loop, though I would like to speed things up.

The other thing I tried was using Worker Threads. However spawning that many threads was obviously even slower than the previous approach.

I will probably have to find a way to batch messages before sending them to the worker process unless there is a way of achieving this in Node that I have not tried yet. Any ideas?

Copyright Notice:Content Author:「dislick」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61250251/quickest-way-to-call-a-computationally-expensive-function-many-times-in-node-js

More about “Quickest way to call a computationally expensive function many times in Node.js” related questions

Quickest way to call a computationally expensive function many times in Node.js

I have a function that is somewhat computationally expensive, we are talking about 2–5ms here. function heavyWork() { // ... } heavyWork() gets called a couple hundert times by different parts ...

Show Detail

Call an expensive function periodically inside a loop

I call a computationally expensive function inside a loop: for( int j = 0; j < Max; ++j ) { // ... processQueuedEvents(); // Computationally expensive call // ... } However

Show Detail

Is this recursive random string generation computationally expensive?

I have created a function to generate a unique referral code for a user when they sign up, I want to ensure uniqueness so I check if it already exists, if it does then I call the function again

Show Detail

Parallel execution of computationally expensive map

I am new to the ReactiveX library (I use its scala variant, RxScala). I have an Observable that emits values at high rate. I would like to apply a function to all values of the Observable (map). The

Show Detail

useMemo computationally expensive calculations

I've created a sandbox using useMemo to optimize a mock expensive function follow Kent C Dodds example from this post. Memoisation doesn't seem to be working. Any ideas why? https://codesandbox.io/s/

Show Detail

useMemo computationally expensive calculations

I've created a sandbox using useMemo to optimize a mock expensive function follow Kent C Dodds example from this post. Memoisation doesn't seem to be working. Any ideas why? https://codesandbox.io/s/

Show Detail

What makes non linear functions computationally expensive in hardware (e.g. FPGA)?

I've read some articles that state non-linear functions (like exponentials) are computationally expensive. I was wondering what makes them computationally expensive. When referring to 'computatio...

Show Detail

Are LogCat calls computationally expensive in Android?

I want to do some performance benchmarking of some blocks of code in my Android app. My plan is to measure System.nanoTime() at various points and then spit out the difference to the Logcat. Howe...

Show Detail

How can I efficiently sort an array based on a computationally expensive key function?

I asked this question about sorting an array with a key function, where it turned out that there is no way to avoid using a comparison function. The problem is that I have a computationally expen...

Show Detail

How computationally expensive is `exp`?

I am currently hearing a lecture about automatic speech recognition (ASR). The last lecture was about vector quantization (VQ) and k nearest neighbors (kNN) as well as binary trees and gaussian mix...

Show Detail