Last active
August 29, 2015 14:23
-
-
Save mtoso/fb19c1bdad4856c657fb to your computer and use it in GitHub Desktop.
A simple javascript class for speed testing
This file contains 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
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); | |
} | |
}; | |
/* Use: | |
var test = new SpeedTest(<function>, <params>, <# of iterations>); | |
test.startTest(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment