Created
May 20, 2021 05:25
-
-
Save craftybones/3834c149698db0a4f247335341e602fa 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
| 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