I am trying to time the execution of my function in milliseconds. I use performance.now() in order to do that. I am able to get the time on the first run, but on the second, third, and so on runs I get 0 milliseconds. Here is an example:
function someFunction (){
var t0 = performance.now();
//Function calculations
var t1 = performance.now();
Console.log(t1 - t0);
}
I launch the function onclick. It works when I first launch the page. It stops working on the second click. t0 and t1 get the same values and when I subtract them I get 0 for the time. Is there anyway around it? I don't necessarily need to use performance.now(). I just want to measure time in milliseconds.
Thank you.
Update I think it has everything to do with the speed. For example:
<html>
<script type="text/javascript">
function someFunction (){
var t0 = performance.now();
console.log(t0);
//Function calculations
//Some loop
var counter = 0;
for (i = 0; i < 1000000; i++) {
counter ++;
}
var t1 = performance.now();
console.log(t1);
console.log(t1 - t0);
}
</script>
<button type="button" onclick="someFunction()">Click me</button>
</hmtl>
Works as I would expect, but with the loop for (i = 0; i < 1000; i++)
it doesn't.
Thank you for the pointers in the right direction.
The actual code you use will change the results here, and why the test comes to 0 as the result is a matter of speculation without that.
That said, micro benchmarks in JavaScript these days are subject to optimizations. For example:
Spiffy!
Let's say
spiffy()
deterministically always outputs the same result. The optimizer is allowed to effectively run this as:Which turns
into a useless test result.
If you've got a bona-fide performance problem in your JavaScript app, I would profile it when it's running slower than molasses and analyze the busiest portions of your code. And I don't mean micro benchmarks, but examining run-time, look at the algorithm you're using in that section and see if there might be a better one for your circumstances, and finally, ask someone else about the actual code in question, in the same context it's running in.