Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda