Skip to content

Instantly share code, notes, and snippets.

@craftybones
Created May 20, 2021 05:25
Show Gist options
  • Select an option

  • Save craftybones/3834c149698db0a4f247335341e602fa to your computer and use it in GitHub Desktop.

Select an option

Save craftybones/3834c149698db0a4f247335341e602fa to your computer and use it in GitHub Desktop.
const firstAlgo = (x) => {
if (x % 15 == 0) {
return { numberOfChecks: 1, val: "fizzbuzz" };
}
if (x % 5 == 0) {
return { numberOfChecks: 2, val: "buzz" };
}
if (x % 3 == 0) {
return { numberOfChecks: 3, val: "fizz" };
}
return { numberOfChecks: 3, val: x.toString() };
}
const secondAlgo = (x) => {
if (x % 3 == 0) {
if (x % 5 == 0) {
return { numberOfChecks: 2, val: "fizzbuzz" };
}
return { numberOfChecks: 2, val: "fizz" };
}
if (x % 5 == 0) {
return { numberOfChecks: 2, val: "buzz" };
}
return { numberOfChecks: 2, val: x.toString() };
}
const benchmark = (fn,times) => {
let total = 0;
for (let x = 1; x <= times; x++) {
const { numberOfChecks } = fn(x);
total += numberOfChecks;
}
console.log(times, total);
}
benchmark(firstAlgo, 100);
benchmark(secondAlgo, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment