FizzBuz

JavaScript performance comparison

Revision 10 of this test case created

Info

better fizz

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var aBillion = 1000000000;
    var number = 1;
    var log = function(value) {
      // console.log(value);
    };
   
    function fizzBuzzOriginal(number) {
      if (number % 3 !== 0) {
        if (number % 5 !== 0) {
          return number;
        }
        return 'buzz';
      }
      if (number % 5 !== 0) {
        return 'fizz';
      }
   
      return 'fizzbuzz';
    };
   
    var fb_switch = {
      0: 'fizzbuzz',
      1: 1,
      2: 1,
      3: 'fizz',
      4: 1,
      5: 'buzz',
      6: 'fizz',
      7: 1,
      8: 1,
      9: 'fizz',
      10: 'buzz',
      11: 1,
      12: 'buzz',
      13: 1,
      14: 1
    };
   
    function djsFizzBuzz(number) {
      var fb = fb_switch[number % 15];
      return fb;
    }
   
    function bitwiseFizzBuzz(number) {
      return [number, "fizz", "buzz", "fizzbuzz"][!(number % 5) << 1 ^ !(number % 3)];
    };
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Original
number = 1;
while (number <= aBillion) {
  log(fizzBuzzOriginal(number));
  number++;
}
pending…
DJS
number = 1;
while (number <= aBillion) {
  log(djsFizzBuzz(number));
  number++;
}
pending…
Bitwise
number = 1;
while (number <= aBillion) {
  log(bitwiseFizzBuzz(number));
  number++;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment