Quickly test performance of a function on the console (in the browser) with:
var i = performance.now();
yourFunction();
performance.now()-i;
Or make a helper function, like this:
function performanceTest(testFunction, iterations){
var sum = 0;
var start = performance.now();
for(var i = 0; i<iterations;i++){
testFunction();
}
var time=performance.now()-start;
return time;
}
And use it like this:
performanceTest(function(){Math.random()*Math.random()}, 1000)
In NodeJS you would need to use process.hrtime()
instead of performance.now()
and it behaves a little differently.
Didn't know that existed. I always used new Date().
Good tip :O