0
function heavyCalculationTask(param) {}

for(let i = 0; i<5000;i++) {
  heavyCalculationTask(i)
}

I'm wondering if we can somewhat executing heavyCalculationTask parallelly, to fully utilize the number of cores we have on the machine, instead of letting it looping one by one?

Initially I thought of turning heavyCalculationTask into returning promise and eventually use Promise.all to wait for all the task to be completed, but guess it doesn't help, turning it into Promise just changing the order of execution, in terms of total duration spent still similar if not worse

Another idea is to move heavyCalculationTask into it's own file and using worker_thread to execute? But before jumping into that I'm wondering if there is any other alternatives

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • Use [WebWorkers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) if this is on the client or [child process](https://nodejs.org/api/child_process.html) for Node.js. I have used and recommend [tiny-worker](https://github.com/avoidwork/tiny-worker) which emulates WebWorker's API on Node.js – nicholaswmin Mar 08 '22 at 10:21

1 Answers1

0

Initially I thought of turning heavyCalculationTask into returning promise and eventually use Promise.all to wait for all the task to be completed, but guess it doesn't help, turning it into Promise just changing the order of execution...

Correct, it would still be running on the same thread. In fact, it wouldn't even change the order of execution; the promise executor function is run synchronously when you call new Promise. (It just has a slight effect on when you can see the result.)

Another idea is to move heavyCalculationTask into it's own file and using worker_thread...

Yes, worker_threads is how you do it on Node.js. (On the web platform, the equivalent is web workers.) Each worker has its own thread, separate from the main thread, and so can do things truly in paralle withl each other and with the main thread (or not, depending on how the operating system schedules the threads). Another option on Node.js is to use full child processes, but if you just need a separate thread, worker threads are the simpler option.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875