Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
FizzBuzz v1
(version: 1)
Comparing performance of:
A vs B vs C
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function fizzBuzzA(number) { let result = ''; if (number % 3 == 0) { result += 'Fizz'; } if (number % 5 == 0) { result += 'Buzz'; } if (!result) { result += number.toString(); } return result; } function fizzBuzzB(number) { if (number % 15 == 0) { return 'FizzBuzz'; } if (number % 3 == 0) { return 'Fizz'; } if (number % 5 == 0) { return 'Buzz'; } return number.toString(); } function fizzBuzzC(n) { if (n % 3 == 0) if (n % 5 == 0) return 'FizzBuzz'; else return 'Fizz'; if (n % 5 == 0) return 'Buzz'; return n.toString(); }
Tests:
A
const iterations = 1000000; for (let i = 0; i < iterations; i++) { fizzBuzzA(i + 1); }
B
const iterations = 1000000; for (let i = 0; i < iterations; i++) { fizzBuzzB(i + 1); }
C
const iterations = 1000000; for (let i = 0; i < iterations; i++) { fizzBuzzC(i + 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
A
B
C
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition: FizzBuzz** FizzBuzz is a classic programming exercise where you need to write a function that prints out numbers from 1 to `n` (inclusive), but with some twists. If the number is divisible by 3, print "Fizz" instead of the number. If it's divisible by 5, print "Buzz". If it's divisible by both 3 and 5, print "FizzBuzz". **Benchmark Variations** There are three variations of FizzBuzz implemented in this benchmark: 1. `fizzBuzzA(number)`: This function takes a single argument `number` and returns the corresponding string according to the FizzBuzz rules. 2. `fizzBuzzB(number)`: This function also takes a single argument `number` but uses a different approach. Instead of checking for divisibility by 3 and 5 separately, it checks if the number is divisible by their least common multiple (LCM), which is 15. If it's divisible by 15, it returns "FizzBuzz". Otherwise, it checks for individual divisibility and returns either "Fizz" or "Buzz". 3. `fizzBuzzC(n)`: This function takes a single argument `n` and uses an if-else chain to check for the FizzBuzz conditions. **Options Compared** The benchmark compares three different approaches: * `fizzBuzzA(number)` vs. `fizzBuzzB(number)`: This comparison tests how performance is affected by the choice of approach when dealing with individual divisibility checks. * `fizzBuzzB(number)` vs. `fizzBuzzC(n)`: This comparison tests how performance is affected by the use of an if-else chain versus a more explicit LCM check. **Pros and Cons** 1. `fizzBuzzA(number)`. * Pros: Simple, easy to understand, and maintain. * Cons: May be slower due to individual divisibility checks. 2. `fizzBuzzB(number)`. * Pros: Can be faster for larger numbers since it uses a single check for the LCM. * Cons: May be less intuitive or harder to understand for those unfamiliar with the LCM concept. 3. `fizzBuzzC(n)`. * Pros: Simple and easy to understand, but may not be as efficient as `fizzBuzzB(number)` due to individual checks. **Libraries Used** There are no libraries used in these benchmark variations. **Special JS Features/Syntax** There are no special JavaScript features or syntax used beyond standard ES5/ES6 syntax. **Other Alternatives** If you're looking for alternative implementations, here are a few options: * `fizzBuzzA(number)` could be optimized using SIMD instructions (e.g., SSE or AVX) to perform the individual divisibility checks simultaneously. * `fizzBuzzB(number)` could use a different LCM calculation approach, such as the Euclidean algorithm, which might offer better performance for certain inputs. * `fizzBuzzC(n)` could be optimized using memoization or caching to store previously computed results and avoid redundant calculations. Keep in mind that optimizations like these would likely require additional code changes and may not provide significant performance benefits unless you're dealing with extremely large inputs.
Related benchmarks:
pedroac Fizz Buzz
pedroac Fizz Buzz array allocation
pedroac Fizz Buzz cache 6
Fizbuzz v2
Comments
Confirm delete:
Do you really want to delete benchmark?