0

Is it a correct way to count the speed of a specific code using the Date object?

var start = +new Date(); //get start time

    /*
     *    
     *    all the commands here
     *
     */

var stop = +new Date(); //get stop time
console.log(stop - start);

Or is there another preferable way to get results. I guess this way is very influanced by which browser you test it on and so forth...

Pete D
  • 777
  • 5
  • 15
  • For any really good description of performance you should use a profiler, that being said this is a decent method; returns execution speed in milliseconds. – Hunter McMillen Jun 26 '13 at 15:09
  • possible duplicate of [How to measure time taken by a function to execute](http://stackoverflow.com/questions/313893/how-to-measure-time-taken-by-a-function-to-execute) and http://stackoverflow.com/q/15342687/218196, http://stackoverflow.com/q/4784745/218196 – Felix Kling Jun 26 '13 at 15:09
  • 1
    depends on how long the event you're timing is. for more than a few seconds, it's pretty accurate. if you're timing `x = 1 + 1;`, then you're going to get wildly inconsistent results. – Marc B Jun 26 '13 at 15:09
  • or any kind of IO operation, those results vary by quite a lot. – Hunter McMillen Jun 26 '13 at 15:10
  • `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. – Laurent S. Jun 26 '13 at 15:12

4 Answers4

3

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.

Esailija
  • 138,174
  • 23
  • 272
  • 326
2

Better use the console timer: https://developer.mozilla.org/en-US/docs/Web/API/console

console.time("answer time");
alert("Click to continue");
console.timeEnd("answer time");
Wolfgang Kuehn
  • 12,206
  • 2
  • 33
  • 46
1

Yes there is a better way, check this What is the best way to profile javascript execution?

Basically:

console.profile([title])
...
console.trace()
...
console.profileEnd ()
Community
  • 1
  • 1
Ezequiel Gorbatik
  • 2,475
  • 1
  • 16
  • 19
1

Here's an alternative solution if you use Firefox: you can use the profiler to know the exact time it takes for your javascript to run.

How to use it (the fast way):
1) Bring up the profiler by hitting shift-f5 in firefox.
2) Start profiling and reload your web page
3) Stop profiling, it should then load the profiling data
4) expand the little arrow looking like : "> (total)", it will show up all the scripts that have been run during the page load, sorted by execution time (in ms)
5) Find your script, the time is on the left side of the tool, and expressed in MS.