FizzBuz

JavaScript performance comparison

Revision 12 of this test case created and last updated

Info

better fizz

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var aMillion = 1000000;
    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];
   
      if (fb === 1) {
        return number;
      }
   
      return fb;
    }
   
    var acc = 810092048;
    function bitwiseFizzBuzz(i) {
      var c = acc & 3, a = 'fizzbuzz';
      if (c === 0) {
        a = i;
      } else if (c === 1) {
        a = 'fizz';
      } else if (c === 2) {
        a = 'buzz';
      }
   
      acc = acc >> 2 | c << 28;
      return a;
    }
};

Benchmark.prototype.teardown = function() {
    number = 1;
    acc = 810092048;
};
</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
while (number <= aMillion) {
  log(fizzBuzzOriginal(number));
  number++;
}
pending…
DJS
while (number <= aMillion) {
  log(djsFizzBuzz(number));
  number++;
}
pending…
Bitwise
while (number <= aMillion) {
  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