Last active
December 8, 2016 22:51
-
-
Save sethmcleod/8e50cbae440e7012b2b2 to your computer and use it in GitHub Desktop.
Javascript constructor for testing function execution speeds
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
function speedTest(testImplement, testParams, repetitions = 10000) { | |
// code to be tested | |
this.testImplement = testImplement; | |
// parameters needed for code | |
this.testParams = testParams; | |
// optional number of reps; higher = better average | |
this.repetitions = repetitions; | |
this.average = 0; | |
} | |
// add methods to Prototype for better preformance | |
speedTest.prototype = { | |
startTest: () => { | |
let beginTime, endTime, sumTimes = 0; | |
for (let 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 executions across ' + this.repetitions + ': ' + this.average); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use, create a new test:
Then call the startTest method: