FizzBuzz One Liner Against Loop

JavaScript performance comparison

Test case created by BeejLuig

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const fizzBuzzOneLine = max => Array(max)
    .fill()
    .map((_,i) => String(
      (
        (i + 1) % 3 === 0 ? 'Fizz' : '')
        + ((i + 1) % 5 === 0 ? 'Buzz' : '')
      ) || i + 1
    )
  
  const fizzBuzzLoop = max => {
    const result = [];
    for (let i=1; i<= max; i++) {
      if (i % 15 === 0) {
        result.push('FizzBuzz')
      } else if (i % 5 === 0) {
        result.push('Buzz')
      } else if (i % 3 === 0) {
        result.push('Fizz')
      } else {
        result.push(String(i))
      }
    }
    return result;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
One Liner
fizzBuzzOneLine(100000)
pending…
Loop
fizzBuzzLoop(100000)
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments