Skip to content

Instantly share code, notes, and snippets.

@jsmeltzer
Created June 24, 2015 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmeltzer/49771ea0f93f8406f87d to your computer and use it in GitHub Desktop.
Save jsmeltzer/49771ea0f93f8406f87d to your computer and use it in GitHub Desktop.
JavaScript Speed Test Class From Code School
/*
Speed Test Class
Tests a code block X number of times and returns an average of the execution time
https://www.codeschool.com/courses/javascript-best-practices
*/
var SpeedTest = function(testImplement,testParams,repetitions){
this.testImplement = testImplement;
this.testParams = testParams;
this.repetitions = repetitions || 10000;
this.average = 0;
};
SpeedTest.prototype = {
startTest: function(){
if( this.testImplement( this.testParams ) === false ){
alert("Yo, man, that test failed with those parameters.");
return;
}
var beginTime, endTime, sumTimes = 0;
for (var i = 0, x = this.repetitions; i < x; i++){
beginTime = +new Date();
this.testImplement( this.testParams );
endTime = +new Date();
sumTimes += endTime - beginTime;
}
this.average = sumTimes / this.repetitions;
return console.log("Average execution across " + this.repetitions + ": " + this.average);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment