Created
July 7, 2011 15:09
-
-
Save asawilliams/1069717 to your computer and use it in GitHub Desktop.
JavaScript function for comparing performance of multiple functions. Pass in an object in this format {name: functionName, ...}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function perf(fo) { | |
var results = []; | |
// perform test | |
for(key in fo) { | |
var f = fo[key]; | |
var start = new Date().getTime(); | |
for(var j=0;j<10000;j++) { | |
f(); | |
} | |
var end = new Date().getTime(); | |
// get the operations per second | |
results[key] = {ops: Math.round(10000/((end-start)/1000))}; | |
} | |
var fastest = 0; | |
var slowest = 0; | |
// find fastest and slowest | |
for(key in results) { | |
if(fastest < results[key].ops) { | |
fastest = results[key].ops; | |
} | |
if(slowest == 0 || slowest > results[key].ops){ | |
slowest = results[key].ops | |
} | |
} | |
// display results | |
for(key in results) { | |
var metrics; | |
if(results[key].ops == fastest) { | |
metrics = "fastest"; | |
} else { | |
metrics = ((fastest - results[key].ops)/fastest)*100+'%'; | |
if(results[key].ops == slowest){ | |
metrics+=" slowest"; | |
} else { | |
metrics+=" slower"; | |
} | |
} | |
console.log(key+":"+results[key].ops+ "/ops "+metrics); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment