Well generally yes, if you are doing painting, you can factor in a rough painting time as well with:
var start = +new Date();
//Do dom methods draw to canvas etc
setTimeout( function() {
var stop = +new Date();
console.log( stop - start - 13 );
}, 13 );
Also, make sure you call each test as a function and warm-up the functions before timing them instead of doing the code in-line.
function test1() {
//ops
}
function test2() {
//ops
}
var l = 1e5;
while (l--) {
//warmup
test1();
test2();
}
var start = +new Date();
var l = 1e5;
while (l--) {
test1();
}
var stop = +new Date();
console.log(stop - start);
var start = +new Date();
var l = 1e5;
while (l--) {
test2();
}
var stop = +new Date();
console.log(stop - start);
You should note that this setup invites JIT optimizations (which is why it was done in the first place) so you should look out for dead code elimination.
x = 1 + 1;, then you're going to get wildly inconsistent results.I guess this way is very influenced by which browser you test it on and so forth...Javascript execution is anyway influenced by the client configuration, was it the browser, the CPU or the available RAM.