title: Why measuring execution time of a function call in Javascript leads to inconsistent results?
body: <p>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.</p>
  <p>I have made a toy jsfiddle to illustrate my problem.</p>
  <p><a href="https://jsfiddle.net/8hgdzwc5/" rel="nofollow noreferrer">https://jsfiddle.net/8hgdzwc5/</a></p>
  <pre><code>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 &lt; N; ++i){
          for(let j = 0; j &lt; 4; ++j){
              let x = 0;
            let y = 1;
            let z = x/y;
            let str = &quot;cat&quot;;
            let str2 = &quot;cat&quot; + &quot;mouse&quot;;
          }
      } 
  }
  </code></pre>
  <p>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 <strong>more</strong> time for 4000 iterations than 50000 iterations. Why and how can I measure execution times of functions and make inferences about performance more accurately?</p>