To measure a function’s execution time in JavaScript, you can subtract the timestamp taken before the function runs from the timestamp taken after it finishes. Most people use new Date().getTime() to get a timestamp, but if your function runs in less than 1 millisecond, this won’t work, because the smallest unit of a timestamp is the millisecond. So I recommend the Window.Performance API. It gives you the number of milliseconds since the page started loading, with precision down to a thousandth of a millisecond.
The example code below runs say_hi 100 times and calculates the average execution time (in milliseconds) of those 100 runs.
function timeit(func) {
var sum = 0;
for(var i = 0; i < 100; i++) {
var start = performance.now();
func()
var end = performance.now();
sum += (end - start)/100;
}
return sum;
}
function say_hi() {
console.log("Hi");
}
console.log(timeit(say_hi))