3

I am doing some JavaScript exercise and thinking about ways to improve my solution(algorithm) to the exercise. I am thinking of calculating runtime speed after tweaking the code so I will know how much the speed is improved. I searched and found this method and think I can do the same. Here is what I did,

var d = new Date();
var startTime = d.getTime();
var endTime;

function testTargeAlgorithm(){
  ....
  ....
}

testTargetAlgorithm();

endTime = d.getTime();
console.log(endTime-startTime);

It's a very simple algorithm so I don't expect there will be notable difference between the time. But if millisecond cannot measure the speed improvement, what else can I do?

Community
  • 1
  • 1
Bao
  • 617
  • 2
  • 10
  • 17
  • 2
    Use a profiler built in to Firebug or WebKit debugger. – marekful Feb 20 '13 at 16:39
  • Add a loop so you call your function 1000 or more times. On the other hand, if the difference is <1ms, it doesn't really matter than much, does it? – AndrewR Feb 20 '13 at 16:43
  • @AndrewR Yeah. In practice, it probably doesn't matter that much. But for the sake of learning, I think it's helpful to know what each tweak does to the program: does it improve the speed or otherwise? – Bao Feb 21 '13 at 15:24
  • 1
    http://jsperf.com/ allows you to enter different js snippets and test their speed. Also, http://jsperf.com/browse has a searchable list of tests other user have created that might help you find the fastest way of doing things. – AndrewR Feb 21 '13 at 19:58

3 Answers3

4

You can use performance.now() if the engine supports it. This gives a time in milliseconds, with sub-millisecond precision, since the page loaded or app started.

performance.now() // 26742.766999999956

I know Chrome supports it, but not sure about other browsers, node.js, or other engines standalone js engines.


Or you can run your code many times in a loop, and measure the total time taken.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 1
    Thanks. I am using Node.js as the interpreter and I found it has a similar function `process.hrtime()` that returns a two-element array: `[seconds, nanoseconds]`. With that nanosecond precision, I can see the difference after each tweak now. Another new problem being the deviation of each run: even without tweaking, the difference varies each time the program runs. So what I did is to get an average of running 10000 times to reduce deviation. – Bao Feb 21 '13 at 15:17
2

Run the same function again and again.

var startTime = (new Date()).getTime();
for(var i=0;i<1000;i++) {
    testTargeAlgorithm()
}
var endTime = (new Date()).getTime();
console.log(endTime-startTime);

edited to reflect suggestions, thanks Marcel

deramko
  • 2,807
  • 1
  • 18
  • 27
  • 1
    I tried but it seemed millisecond just couldn't catch the subtle difference even if I run the algorithm for 1000000 times. I guess that the algorithm is too simple for this kind of measurement. However, with a nanosecond approach (`process.hrtime()` of Node.js), the difference in time is successfully captured. – Bao Feb 21 '13 at 15:20
  • There's a bug in this example. "var endtime" needs to be "var endTime". SO wouldn't let me do a one character edit. – broccoli2000 Sep 11 '16 at 18:24
1

I ended up using process.hrtime() to provide nanosecond precision for measuring runtime performance. Note this method only works with Node.js. In Chrome & Firefox, you can use performance.now().

Even running same algorithm/function, the returned time difference still varies (in nanosecond unit tho) presumably due to CPU usage and other unknown effects, so it is suggested to run a good number of times and calculate the average. For example:

function calAvgSpeed(timesToRun, targetAlgorithm){

var diffSum = 0;
for(var i = 1; i <= timesToRun; i++){
    var startTime = process.hrtime();
    targetAlgorithm();
    var diff = process.hrtime(startTime);
    diffSum += diff[1];
   }
   return Math.floor(diffSum / times);
}
Bao
  • 617
  • 2
  • 10
  • 17