Skip to content

Instantly share code, notes, and snippets.

@jafar-jabr
Last active February 27, 2022 14:20
Show Gist options
  • Save jafar-jabr/816c9b017f41b4f255f4f913427c285f to your computer and use it in GitHub Desktop.
Save jafar-jabr/816c9b017f41b4f255f4f913427c285f to your computer and use it in GitHub Desktop.
performanceCompare
const performanceCompare = (callBack1, callBack2, amplifier=10000) => {
callBack1();
callBack2();
const t1 = performance.now();
callBack1();
const t2 = performance.now();
const t3 = performance.now();
callBack2();
const t4 = performance.now();
const perform1 = ~~(t2-t1);
const perform2 = ~~(t4-t3);
if(perform1 || perform2) {
return `${callBack1} took ${perform1} milliseconds while ${callBack2} took ${perform2} milliseconds`
}
const t5 = performance.now();
for(let i = 0; i < amplifier; i+=1) {
callBack1();
}
const t6 = performance.now();
const t7 = performance.now();
for(let j = 0; j < amplifier; j+=1) {
callBack2();
}
const t8 = performance.now();
const perform3 = ~~(t6-t5);
const perform4 = ~~(t8-t7);
return `iterating ${amplifier} times ${callBack1} took ${perform3} milliseconds while ${callBack2} took ${perform4} milliseconds`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment