-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpeedTest.js
More file actions
29 lines (26 loc) · 1.06 KB
/
SpeedTest.js
File metadata and controls
29 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Accurately measure how long each function takes to run, a better solution than the console.time()/console.timeEnd() functions
//1st param is for the function to test, 2nd param is params required by 1st param's function, 3rd param is for how many reps the test should run
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('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);
}
};
//Credit to Codeschool for the code