Last active
April 9, 2021 16:20
-
-
Save boospot/18845e7b8bd107cdb1b8052fc4ad2924 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 copmareRuntime(functionSet= {}, params=[], iterations=10000, averageOf = 1){ | |
const report = {}; | |
report.results = []; | |
let slowestTime = 0; | |
let fastestTime = 0; | |
let slowestTotalTime = 0; | |
let fastestTotalTime = 0; | |
let fastestFunction = ''; | |
let slowestFunction = ''; | |
for (let key in functionSet ){ | |
const result = {}; | |
result.function = key; | |
const func = functionSet[key]; | |
let totalTimeTaken = 0; | |
for(let i = 1; i <= averageOf; i++){ | |
// Get a hold of start | |
let t1 = Date.now(); | |
// call the function for iteration | |
for (let i = 0 ; i <=iterations; i++){ | |
func(...params); | |
} | |
const t2 = Date.now() - t1; | |
totalTimeTaken += t2; | |
} | |
const timeTaken = Math.round(totalTimeTaken / averageOf); | |
if( !fastestTime || timeTaken < fastestTime){ | |
fastestTime = timeTaken; | |
fastestFunction = key; | |
} | |
if( !slowestTime || timeTaken > slowestTime){ | |
slowestTime = timeTaken; | |
slowestFunction = key; | |
} | |
if( !fastestTotalTime || totalTimeTaken < fastestTotalTime){ | |
fastestTotalTime = totalTimeTaken; | |
} | |
if( !slowestTotalTime || totalTimeTaken > slowestTotalTime){ | |
slowestTotalTime = totalTimeTaken; | |
} | |
result.averageTime = timeTaken; | |
result.totalTime = totalTimeTaken; | |
report.results.push(result); | |
} | |
let fastestBy= (slowestTotalTime - fastestTotalTime) / fastestTotalTime; | |
fastestBy = !isNaN(fastestBy) ? fastestBy : 0; | |
report.fastest = fastestFunction; | |
report.slowest = slowestFunction; | |
report.leadTimeAverage = slowestTime - fastestTime; | |
report.leadTimeTotal = slowestTotalTime - fastestTotalTime; | |
report.fasterBy = `${Math.round(fastestBy * 100)}%`; | |
report.iterations = iterations; | |
report.averageOf = averageOf; | |
console.log(report); | |
return report; | |
} | |
module.exports = copmareRuntime; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment