Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda