o4zmzw
Last Updated: July 05, 2018
·
17.24K
· justgoscha

Quick performance test on JavaScript console

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.

4 Responses
Add your response

16021

Didn't know that existed. I always used new Date().

Good tip :O

over 1 year ago ·
16097

Coooool

over 1 year ago ·
16103

Nice to know! :)

You could add the info that performance.now() doesn't call console.log() implicit, what was my expectation after reading your code example.

over 1 year ago ·
30115

This is very nice and helpful http://windowstuts.net/fileexplorer you make it very simple according to your ways. Just give some other help for the new persons.

4 months ago ·