Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@boospot
Last active April 9, 2021 16:20
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 boospot/18845e7b8bd107cdb1b8052fc4ad2924 to your computer and use it in GitHub Desktop.
Save boospot/18845e7b8bd107cdb1b8052fc4ad2924 to your computer and use it in GitHub Desktop.
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