FizzBuz

JavaScript performance comparison

Revision 9 of this test case created

Info

better fizz

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function fizzBuzz1(number) {
      switch (number % 15) {
        case 0:
          return 'fizzbuzz';
        case 3:
          return 'fizz';
        case 5:
          return 'buzz';
        case 6:
          return 'fizz';
        case 9:
          return 'fizz';
        case 10:
          return 'buzz';
        case 12:
          return 'buzz';
        default:
          return number;
      };
    }
   
    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
    };
};
</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
Fizzbuz
function fizzbuzz(i) {
  var lookup = [null, "fizz", "buzz", "fizzbuzz"];
  var mask = (!(i % 5) << 1) ^ (!(i % 3));
  return lookup[mask] || i;
}
pending…
Old
function fizzBuzz(number) {

  if (number % 3 !== 0) {
    if (number % 5 !== 0) {
      return number;
    }
    return 'buzz';
  }
  if (number % 5 !== 0) {
    return 'fizz';
  }

  return 'fizzbuzz';
};
pending…
Bitwise2
function fizzbuzz(i) {
  var lookup = ['fizzbuzz', null, null, 'fizz', null, 'buzz', 'fizz', null, null, 'fizz', 'buzz', null, 'fizz', null, null];

  return lookup[i % 15] || i;
}
pending…
function fizzBuzz(i) {
  var lb = (810092048 >> (((i - 1) << 1) % 30)) & 3;

  return lb ? [null, "Fizz", "Buzz", "FizzBuzz"][lb] : i;
}
pending…
maybe faster
function fizzBuzz(i) {
  //return lookup[acc & 3] || i;
  switch ((810092048 >> (((i - 1) << 1) % 30)) & 3) {
    case 0:
      return i;
    case 1:
      return 'Fizz';
    case 2:
      return 'Buzz';
    case 3:
      return 'FizzBuzz';
  }

  // return [null, "Fizz", "Buzz", "FizzBuzz"][lb]
}
pending…
maybe even faster
function fizzBuzz(i) {
  //return lookup[acc & 3] || i;
  var lb = (810092048 >> (((i - 1) << 1) % 30)) & 3;

  if (lb === 0) {
    return i;
  } else if (lb === 1) {
    return 'Fizz';
  } else if (lb === 2) {
    return 'Buzz';
  }

  return 'FizzBuzz';
}
pending…
clouserized
function fizzbuzz(a) {
  return [null, "fizz", "buzz", "fizzbuzz"][!(a % 5) << 1 ^ !(a % 3)] || a
};
pending…
DJS
//DJS lookup attempts

function fizzBuzz3(number) {
  var fb = fb_switch[number % 15];
  return fb;
}
pending…
butt pirate
function fizzBuzz(i) {
  var lb = (810092048 >> (((i - 1) << 1) % 30)) & 3;

  //return lookup[acc & 3] || i;
  return [null, "Fizz", "Buzz", "FizzBuzz"][lb] || i;
}
pending…
super butt pirate
function fizzbuzz(a) {
  return [a, "fizz", "buzz", "fizzbuzz"][!(a % 5) << 1 ^ !(a % 3)];
};
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