Skip to content

Instantly share code, notes, and snippets.

@appkr
Last active August 29, 2015 14:14
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 appkr/46420c0034ae00ec406c to your computer and use it in GitHub Desktop.
Save appkr/46420c0034ae00ec406c to your computer and use it in GitHub Desktop.
JavaScript SpeedTest Class// source http://jsbin.com/fozugi
//noprotect
function SpeedTest(testImplement, testPrams, repetitions) {
this.testImplement = testImplement;
this.testPrams = testPrams;
this.repetitions = repetitions || 10000;
this.average = 0;
}
SpeedTest.prototype = {
startTest: function() {
var beginTime,
endTime,
sumTimes = 0;
for(var i = 0, x = this.repetitions; i < x; i++) {
beginTime = +new Date();
this.testImplement(this.testPrams);
endTime = +new Date();
sumTimes += endTime - beginTime;
}
this.average = sumTimes / this.repetitions;
return console.log("Average execution across " + this.repetitions + ": " + this.average);
}
};
/** test */
function Knight(name, regiment) {
this.name = name;
this.regiment = regiment;
switch(regiment) {
case 1:
this.weapon = "Broadsword";
break;
case 2:
this.weapon = "Claymore";
break;
default:
this.weapon = "WoodSword";
}
}
var firstRegimentNewbs = [
"Grimble Horsehead",
"Jark Winterborn",
"Bunder Ropefist",
"Earst Breadbaker"
];
var firstRegimentKnights = [];
var listForTests = [firstRegimentNewbs, firstRegimentKnights];
var test = function (listOfParams) {
for (var i = 0, x = listOfParams[0].length; i < x; i++) {
listOfParams[1].push(new Knight(listOfParams[0][i], 1));
}
};
var test = new SpeedTest(test, listForTests);
test.startTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment