Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!