FizzBuz

JavaScript performance comparison

Revision 11 of this test case created by Brenden

Info

better fizz

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var aBillion = 1000;
    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;
    }
   
    function bitwiseFizzBuzz(number) {
      return [number, "fizz", "buzz", "fizzbuzz"][!(number % 5) << 1 ^ !(number % 3)];
    };
   
    function bitwiseFizzBuzz2(i) {
      var c, a;
      c = acc & 3;
      if(c === 0) {
        a = i;
      }  else if(c === 1) {
        a = 'fizz';
      } else if(c === 2) {
        a = 'buzz';
      } else {
        a = 'fizzbuzz';
        //a = [null, "fizz", "buzz", "fizzbuzz"][c];
      }
   
      acc = acc >> 2 | c << 28;
   
    return a;
    }
   
    function buttpirate(i) {
      var mask = (!(i % 5) << 1) ^ (!(i % 3));
      if(mask === 0) {
        return i;
      } else {
        return [null, "fizz", "buzz", "fizzbuzz"][mask];
      }
    }
};

Benchmark.prototype.teardown = function() {
    number = 1;
};
</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
var random = (Math.random() * 100) >> 0;
  log(fizzBuzzOriginal(random));
 
pending…
DJS
var random = (Math.random() * 100) >> 0;
  log(djsFizzBuzz(random));
 
pending…
Bitwise
var random = (Math.random() * 100) >> 0;
  log(bitwiseFizzBuzz(random));
 
pending…
Bitwise2
var acc = 810092048;
var random = (Math.random() * 100) >> 0;
log(bitwiseFizzBuzz2(random));
 
pending…
Buttpirate
var random = (Math.random() * 100) >> 0;
log(buttpirate(random));
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