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

172
Visualizações
Why measuring execution time of a function call in Javascript leads to inconsistent results?

I am trying to measure the time it takes to call a (void) function in javascript, but I am getting unexpected results and therefore must be misunderstanding something fundamental.

I have made a toy jsfiddle to illustrate my problem.

https://jsfiddle.net/8hgdzwc5/

function f(){
    let iterations = [10, 20, 30, 4000, 50000]
    let times = []
    for(let N of iterations){
        let startTime = performance.now();
        g(N);
        let endTime = performance.now()
        times.push(endTime - startTime)
    }
    console.log(times);
}

function g(N){
    for(let i = 0; i < N; ++i){
        for(let j = 0; j < 4; ++j){
            let x = 0;
          let y = 1;
          let z = x/y;
          let str = "cat";
          let str2 = "cat" + "mouse";
        }
    } 
}

I expect the execution times I measure and save in the array 'times' to be increasing, however running the fiddle a few times (I hope this is reproducible), it seems to take more time for 4000 iterations than 50000 iterations. Why and how can I measure execution times of functions and make inferences about performance more accurately?

about 4 years ago · Juan Pablo Isaza
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