Timing with javascript performance.now() -
Timing with javascript performance.now() -
i trying time execution of function in milliseconds. utilize performance.now() in order that. able time on first run, on second, third, , on runs 0 milliseconds. here example:
function somefunciton (){ var t0 = performance.now(); //function calculations var t1 = performance.now(); console.log(t1 - t0); }
i launch function onclick. works when first launch page. stops working on sec click. t0 , t1 same values , when subtract them 0 time. there anyway around it? don't need utilize performance.now(). want measure time in milliseconds.
thank you.
update think has speed. example:
<html> <script type="text/javascript"> function somefunciton (){ var t0 = performance.now(); console.log(t0); //function calculations //some loop var counter = 0; (i = 0; < 1000000; i++) { counter ++; } var t1 = performance.now(); console.log(t1); console.log(t1 - t0); } </script> <button type="button" onclick="somefunciton()">click me</button> </hmtl>
works expect, loop for (i = 0; < 1000; i++)
doesn't.
thank pointers in right direction.
the actual code utilize alter results here, , why test comes 0 result matter of speculation without that.
that said, micro benchmarks in javascript these days subject optimizations. example:
function spiffy() { /* long bit of code loops , loops , runs in o(n!) time */ homecoming result; }
spiffy!
let's spiffy()
deterministically outputs same result. optimizer allowed run as:
function spiffy() { homecoming 42; }
which turns
function somefunction (){ var t0 = performance.now(); var result = spiffy(); var t1 = performance.now(); console.log(t1 - t0); }
into useless test result.
if you've got bona-fide performance problem in javascript app, profile when it's running slower molasses , analyze busiest portions of code. , don't mean micro benchmarks, examining run-time, look @ algorithm you're using in section , see if there might improve 1 circumstances, , finally, inquire else actual code in question, in same context it's running in.
javascript timing
Comments
Post a Comment