Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Possible to execute js function parallely?
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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!