fizzbuzz

JavaScript performance comparison

Revision 5 of this test case created

Info

Write a program that prints out the numbers 1 to 100 (inclusive).

If the number is divisible by 3, print Fizz instead of the number. If it's divisible by 5, print Buzz. If it's divisible by both 3 and 5, print FizzBuzz.

Preparation code

<p id="title">
  RESULTS
</p>
<p id="result" style='font-size:14px;'>
</p>
<script>
</script>
<script>
Benchmark.prototype.setup = function() {
    var arr = [],
    z = 1;
    var x = x3 = x5 = 1;
};

Benchmark.prototype.teardown = function() {
    document.getElementById('result').textContent = arr.slice(1,101).join(', ');
};
</script>

Preparation code output

RESULTS

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Modulo
for (var i = 1; i < 101; ++i) {
  if (i % 3 == 0 && i % 5 == 0) arr[i] = 'Fizz Buzz';
  else if (i % 3 == 0) arr[i] = 'Fizz';
  else if (i % 5 == 0) arr[i] = 'Buzz';
  else arr[i] = i;
}
pending…
Individual Counters (FOR MAX PERFORMANCE!!11!11!!!)
for (var i = c3 = c5 = 1 ; i < 101;  ++i) {
 if ( c3 === 3 && c5 === 5 ) { 
     arr[i] =  "Fizz Buzz" ;
     c3 = 0;
     c5 = 0;
 } else if ( c3 === 3 ){                       
     arr[i] = "Fizz";
     c3 = 0;
 } else if ( c5 === 5 ){                       
     arr[i] = "Buzz";
     c5 = 0;
 } else {
     arr[i] = i;
 }
 ++c5;
 ++c3;
}
pending…
Modulo-global-counters
for (z; z < 101; z++) {
  if (z % 3 == 0 && z % 5 == 0) arr[z] = 'Fizz Buzz';
  else if (z % 3 == 0) arr[z] = 'Fizz';
  else if (z % 5 == 0) arr[z] = 'Buzz';
  else arr[z] = z;
}
pending…
Individual Counters (FOR MAX PERFORMANCE!!11!11!!!)-global-counters
for ( x; x < 101;  x++) {
 if ( x3 === 3 && x5 === 5 ) { 
     arr[x] =  "Fizz Buzz" ;
     x3 = 0;
     x5 = 0;
 } else if ( x3 === 3 ){                       
     arr[x] = "Fizz";
     x3 = 0;
 } else if ( x5 === 5 ){                       
     arr[x] = "Buzz";
     x5 = 0;
 } else {
     arr[x] = x;
 }
 ++x5;
 ++x3;
}
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