FizzBuzz if/else vs object

JavaScript performance comparison

Test case created by eprislac

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  'use strict'
  const nums = Array.from(Array(100).keys())

};
</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
if/else
const fizzbuzziness = (num) => {
  if(num % 3 === 0) {
    return 'Fizz'
  } else if(num % 5 === 0) {
    return 'Buzz'
  } else if((num % 3 === 0) && (num % 5 === 0)) {
    return 'FizzBuzz'
  } else {
    return ''
  }
}

nums.map(n => fizzbuzziness(n))
pending…
object
const fizzbuzziness = (num) => {
    const tbl = { 'Fizz': (num % 3 === 0), 'Buzz': (num % 5 === 0) }
    return Object
      .keys(tbl)
      .filter(key => tbl[key])
      .reduce((acc, curr) => { return `${acc}${curr}` }, '')
  }

nums.map(n => fizzbuzziness(n))
pending…
string literal
(() => {
  const fizzbuzziness =
    num => `${ num % 3 === 0 ? 'Fizz' : '' }${ num % 5 === 0 ? 'Buzz' : '' }`
})()
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