FizzBuzz

JavaScript performance comparison

Test case created by Benny

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // recursive `function`
  function f(s, n) {
    if (++n === 102) return;
    s === '' ? console.log(n - 1) : console.log(s); !! (n % 3) ? !! (n % 5) ? f('', n) : f('Buzz', n) : !! (n % 5) ? f('Fizz', n) : f('FizzBuzz', n);
  }
  
  // `while` loop
  function b(n) {
    var i = n;
    $: while (++i) {
      if (i % 3) if (i % 5) console.log(i);
      else console.log('Buzz');
      else if (i % 5) console.log('Fizz');
      else console.log('FizzBuzz');
      if (i >= 100) break $;
    }
    return;
  }
  
  // `for` deductive loop
  function F(n) {
    var i = n,
        f = 'Fizz',
        b = 'Buzz',
        o = '';
    for (; i <= 100; i++) {
      o = !(i % 3) ? !(i % 5) ? f + b : f : !(i % 5) ? b : i;
      console.log(o);
    }
    return;
  }
  
  // `for` loop
  function B(n) {
    var i = n;
    var fiz = 'Fizz';
    var buz = 'Buzz';
    for (; i <= 100; i++)
    if (!(i % 3)) if (!(i % 5)) console.log(fiz + buz);
    else console.log(fiz);
    else if (!(i % 5)) console.log(buz);
    else console.log(i);
    return;
  }

};
</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
Function : operational, using recursion
f('', 1);
pending…
While : logical, using callbacks
b(0);
pending…
For : operational, using deduction
F(1);
pending…
For : logical, using callbacks
B(1);
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.

1 Comment

Benny (revision owner) commented :

Would be interested for someone to test some inverse loops; or to experiment with more readable solutions that are still performant. :)