fizzbuzz

JavaScript performance comparison

Revision 4 of this test case created by K-Bone Stallone

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 = [];
   
};

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 (VANILLA!)
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…
Dirty String Conversion (WTH!)
for (var i = 1, m3, m5; i < 101; ++i) {
  m3 = (i/3).toString().indexOf('.');
  m5 = (i/5).toString().indexOf('.');
  if ( (m3 + m5) === -2 ){
    arr[i] = 'Fizz Buzz';
  } else if ( m3 === -1 ) {
    arr[i] = 'Fizz';
  } else if ( m5 === -1 ) {
    arr[i] = 'Buzz';
  } else {
    arr[i] = i;
  };
}
pending…
Getting Bitwise Freaky (NERfD)
for (var i = 1,m3,m5 ; i < 101;  ++i) {
m3 = ((m3 = i/3) === ( m3 >> 0 ));
m5 = ((m5 = i/5) === ( m5 >> 0 ));
 if ( m3 && m5 ) {     
     arr[i] =  "Fizz Buzz" ;
 } else if ( m3 ){                     
     arr[i] = "Fizz";
 } else if ( m5 ){                     
     arr[i] = "Buzz";
 } else {
     arr[i] = i;
 }
}
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