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

86
Visualizações
Measuring await time in Node

Let's say I have a function in Node

const my_function = async () => {
  await sub_function_1();
  await sub_function_2();
  await sub_function_3();
}

I could profile how long it takes my_function to run with:

let t0 = new Date()
await my_function()
let t1 = new Date()
let elapsed_ms = t1 - t0

But that wouldn't show me how long each of the sub_functions took to run. Similarly, I would add profiling code around each of the sub_functions, but that wouldn't show me which one of the sub-sub functions took the longest amount of time to run.

Is there a way, for a given function call, the gather all of the awaited functions underneath that call, along with how long each took to resolve?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

what about a wrapper function which take a array of functions and runs them sequentially?

//simulating a lengthy request
const sub_function = (time) => {return new Promise((res,rej) => setTimeout( () => res(true),time  ) )}

const new_list = [
    {handler:sub_function(90), alias:"sub_function_1"},
    {handler:sub_function(150), alias:"sub_function_2"},
    {handler:sub_function(350), alias:"sub_function_3"},

]

const calculate_max_time = async list =>{
    const time_passed = []
    for (const func of list) {

        const time = Date.now()
        await func.handler
        const time2 = Date.now()
        time_passed.push({alias:func.alias, value:time2 - time})
    }
    const max_time = time_passed.reduce( (max,func) => max.value > func.value ? max : func)
    console.log(max_time)
}

calculate_max_time(new_list)
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