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

170
Vistas
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 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