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?