FizzBuzz

JavaScript performance comparison

Test case created by adelyte-chris

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const FIZZ = "Fizz"
  const BUZZ = "Buzz"
  
  const RESULTS = [
    '1',
    '2',
    'Fizz',
    '4',
    'Buzz',
    'Fizz',
    '7',
    '8',
    'Fizz',
    'Buzz',
    '11',
    'Fizz',
    '13',
    '14',
    'FizzBuzz',
    '16',
    '17',
    'Fizz',
    '19',
    'Buzz',
    'Fizz',
    '22',
    '23',
    'Fizz',
    'Buzz',
    '26',
    'Fizz',
    '28',
    '29',
    'FizzBuzz',
    '31',
    '32',
    'Fizz',
    '34',
    'Buzz',
    'Fizz',
    '37',
    '38',
    'Fizz',
    'Buzz',
    '41',
    'Fizz',
    '43',
    '44',
    'FizzBuzz',
    '46',
    '47',
    'Fizz',
    '49',
    'Buzz',
    'Fizz',
    '52',
    '53',
    'Fizz',
    'Buzz',
    '56',
    'Fizz',
    '58',
    '59',
    'FizzBuzz',
    '61',
    '62',
    'Fizz',
    '64',
    'Buzz',
    'Fizz',
    '67',
    '68',
    'Fizz',
    'Buzz',
    '71',
    'Fizz',
    '73',
    '74',
    'FizzBuzz',
    '76',
    '77',
    'Fizz',
    '79',
    'Buzz',
    'Fizz',
    '82',
    '83',
    'Fizz',
    'Buzz',
    '86',
    'Fizz',
    '88',
    '89',
    'FizzBuzz',
    '91',
    '92',
    'Fizz',
    '94',
    'Buzz',
    'Fizz',
    '97',
    '98',
    'Fizz',
    'Buzz'
  ]
  
  function report(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
Chris (efficient if-else)
const FIZZBUZZ = `${FIZZ}${BUZZ}`

let result

for (let i = 1; i < 101; i++) {
  if (i % 5 === 0) {
    if (i % 3 === 0) {
      result = FIZZBUZZ
    } else {
      result = BUZZ
    }
  } else {
    if (i % 3 === 0) {
      result = FIZZ
    } else {
      result = i
    }
  }

  report(result)
}
pending…
Taylor (string builder)
let result

for (let i = 1; i < 101; i++) {
  result = ''

  if (i % 3 === 0) {
    result += FIZZ
  }

  if (i % 5 === 0) {
    result += BUZZ
  }

  if (result === '') {
    result = i
  }

  report(result)
}
pending…
David (precomputed switch-case)
const fizzbuzz = number => {
  switch (number) {
    case 1:  return '1' ;
    case 2:  return '2' ;
    case 3:  return 'Fizz' ;
    case 4:  return '4' ;
    case 5:  return 'Buzz' ;
    case 6:  return 'Fizz' ;
    case 7:  return '7' ;
    case 8:  return '8' ;
    case 9:  return 'Fizz' ;
    case 10:  return 'Buzz' ;
    case 11:  return '11' ;
    case 12:  return 'Fizz' ;
    case 13:  return '13' ;
    case 14:  return '14' ;
    case 15:  return 'FizzBuzz' ;
    case 16:  return '16' ;
    case 17:  return '17' ;
    case 18:  return 'Fizz' ;
    case 19:  return '19' ;
    case 20:  return 'Buzz' ;
    case 21:  return 'Fizz' ;
    case 22:  return '22' ;
    case 23:  return '23' ;
    case 24:  return 'Fizz' ;
    case 25:  return 'Buzz' ;
    case 26:  return '26' ;
    case 27:  return 'Fizz' ;
    case 28:  return '28' ;
    case 29:  return '29' ;
    case 30:  return 'FizzBuzz' ;
    case 31:  return '31' ;
    case 32:  return '32' ;
    case 33:  return 'Fizz' ;
    case 34:  return '34' ;
    case 35:  return 'Buzz' ;
    case 36:  return 'Fizz' ;
    case 37:  return '37' ;
    case 38:  return '38' ;
    case 39:  return 'Fizz' ;
    case 40:  return 'Buzz' ;
    case 41:  return '41' ;
    case 42:  return 'Fizz' ;
    case 43:  return '43' ;
    case 44:  return '44' ;
    case 45:  return 'FizzBuzz' ;
    case 46:  return '46' ;
    case 47:  return '47' ;
    case 48:  return 'Fizz' ;
    case 49:  return '49' ;
    case 50:  return 'Buzz' ;
    case 51:  return 'Fizz' ;
    case 52:  return '52' ;
    case 53:  return '53' ;
    case 54:  return 'Fizz' ;
    case 55:  return 'Buzz' ;
    case 56:  return '56' ;
    case 57:  return 'Fizz' ;
    case 58:  return '58' ;
    case 59:  return '59' ;
    case 60:  return 'FizzBuzz' ;
    case 61:  return '61' ;
    case 62:  return '62' ;
    case 63:  return 'Fizz' ;
    case 64:  return '64' ;
    case 65:  return 'Buzz' ;
    case 66:  return 'Fizz' ;
    case 67:  return '67' ;
    case 68:  return '68' ;
    case 69:  return 'Fizz' ;
    case 70:  return 'Buzz' ;
    case 71:  return '71' ;
    case 72:  return 'Fizz' ;
    case 73:  return '73' ;
    case 74:  return '74' ;
    case 75:  return 'FizzBuzz' ;
    case 76:  return '76' ;
    case 77:  return '77' ;
    case 78:  return 'Fizz' ;
    case 79:  return '79' ;
    case 80:  return 'Buzz' ;
    case 81:  return 'Fizz' ;
    case 82:  return '82' ;
    case 83:  return '83' ;
    case 84:  return 'Fizz' ;
    case 85:  return 'Buzz' ;
    case 86:  return '86' ;
    case 87:  return 'Fizz' ;
    case 88:  return '88' ;
    case 89:  return '89' ;
    case 90:  return 'FizzBuzz' ;
    case 91:  return '91' ;
    case 92:  return '92' ;
    case 93:  return 'Fizz' ;
    case 94:  return '94' ;
    case 95:  return 'Buzz' ;
    case 96:  return 'Fizz' ;
    case 97:  return '97' ;
    case 98:  return '98' ;
    case 99:  return 'Fizz' ;
    case 100:  return 'Buzz' ;
  }
}

for (let i = 1; i <= 100; i++) {
  report(fizzbuzz(i))
}
pending…
David (precomputed array)
const FIZZBUZZ = [
  '1',
  '2',
  'Fizz',
  '4',
  'Buzz',
  'Fizz',
  '7',
  '8',
  'Fizz',
  'Buzz',
  '11',
  'Fizz',
  '13',
  '14',
  'FizzBuzz',
  '16',
  '17',
  'Fizz',
  '19',
  'Buzz',
  'Fizz',
  '22',
  '23',
  'Fizz',
  'Buzz',
  '26',
  'Fizz',
  '28',
  '29',
  'FizzBuzz',
  '31',
  '32',
  'Fizz',
  '34',
  'Buzz',
  'Fizz',
  '37',
  '38',
  'Fizz',
  'Buzz',
  '41',
  'Fizz',
  '43',
  '44',
  'FizzBuzz',
  '46',
  '47',
  'Fizz',
  '49',
  'Buzz',
  'Fizz',
  '52',
  '53',
  'Fizz',
  'Buzz',
  '56',
  'Fizz',
  '58',
  '59',
  'FizzBuzz',
  '61',
  '62',
  'Fizz',
  '64',
  'Buzz',
  'Fizz',
  '67',
  '68',
  'Fizz',
  'Buzz',
  '71',
  'Fizz',
  '73',
  '74',
  'FizzBuzz',
  '76',
  '77',
  'Fizz',
  '79',
  'Buzz',
  'Fizz',
  '82',
  '83',
  'Fizz',
  'Buzz',
  '86',
  'Fizz',
  '88',
  '89',
  'FizzBuzz',
  '91',
  '92',
  'Fizz',
  '94',
  'Buzz',
  'Fizz',
  '97',
  '98',
  'Fizz',
  'Buzz'
]

const fizzbuzz = number => {
  return(FIZZBUZZ[number - 1])
}

for (let i = 1; i <= 100; i++) {
  report(fizzbuzz(i))
}
pending…
David (precomputed static array)
for (let i = 0; i < 100; i++) {
  report(RESULTS[i])
}
pending…
Chris (if-else with even check)
const FIZZBUZZ = `${FIZZ}${BUZZ}`

let result

for (let i = 1; i < 101; i++) {
  if (i & 0x01) {
    if (i % 5 === 0) {
      if (i % 3 === 0) {
        result = FIZZBUZZ
      } else {
        result = BUZZ
      }
    } else {
      if (i % 3 === 0) {
        result = FIZZ
      } else {
        result = i
      }
    }
  } else {
    result = i
  }

  report(result)
}
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