I have some Javascript that is reading in some XML. There is an older function which was used to create a JSON object from that data, and I've written a new function that I hope will create that JSON object faster. What is the easiest and best way to determine which function is performing faster? It's a decent amount of data, so it's somewhat important to know. Thanks.
6 Answers
You could use console.time("ID");
and console.timeEnd("ID");
(info here), and look the results in the Chrome Developer Tools or Firebug like so:
console.time("oldFunc");
//oldfunc();
console.timeEnd("oldFunc");
console.time("newfunc");
//newfunc();
console.timeEnd("newfunc");
Also, you could use jsperf
-
1From what I can tell, the order seems to matter: whatever function runs second will have a speed advantage. Proof: run olfunc() against oldfunc(), newfunc() against newfunc().– orsonCommented May 11, 2022 at 17:44
-
@orson That's true. To solve this just run
oldFunc
once before running the two functions again. Commented Jun 2, 2023 at 13:04
Some info on this and code sample here
var startDate = new Date();
// execute your tasks here
var endDate = new Date();
var timeTaken = endDate.getTime() - startDate.getTime();
alert('Time take to execute the script is '+timeTaken+' milliseconds');
(new Date).getTime();
This is how you get current time in milliseconds. Do that before and after execution of code, subtract and you have execution time in miliseconds.
Sample:
var start=(new Date).getTime();
//call your code
alert('Code took '+((new Date).getTime()-start)+'ms');
If your code organisation allows, you can make your call in a for loop, repeating n (let's say 1000) times and divide the time by n at the end.
This way you get the average speed, which is especially helpful if you function varies a lot (like network calls).
-
2... and then do it a thousand times on each version and take the minimum values as result.– BergiCommented Feb 17, 2012 at 17:25
-
Thanks, Bergi! That is good advice, if it that is possible to do in OP's case. Edited answer. Commented Feb 17, 2012 at 17:25
I like John Resigs way of testing the performance of a function:
function runTest(name, test, next){
var runs = [], r = 0;
setTimeout(function(){
var start = Date.now(), diff = 0;
for ( var n = 0; diff < 1000; n++ ) {
test();
diff = Date.now() - start;
}
runs.push( n );
if ( r++ < 4 )
setTimeout( arguments.callee, 0 );
else {
done(name, runs);
if ( next )
setTimeout( next, 0 );
}
}, 0);
}
Is this in the browser or server-side?
If it's server-side, I'd recommend using your shell scripting tool of choice to do the benchmarking (linux has time
, windows has...whatever windows has).
If it's in the browser, then you can always wrap a certain number of iterations (10,000 is usually enough) in:
var start = new Date.getTime();
var runs = 10000;
while (runs) {
// do stuff here
runs--;
}
console.log('Finished in ' + (new Date.getTime() - start) + ' ms.');
var d1 = new Date();
function1();
var d2 = new Date();
console.log("Function 1 : ", d2.getTime() - d1.getTime());
function2();
var d3 = new Date();
console.log("Function 2 : ", d3.getTime() - d2.getTime());