modulo calculations

JavaScript performance comparison

Revision 4 of this test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
normal
var out = '';
for (var i = 1; i <= 100; i++) {
  out = '';
  if (i % 3 == 0) {
    out = 'Fizz';
  }
  if (i % 5 == 0) {
    out += 'Buzz';
  }
  if (!out) {
    out = i;
  }
}
pending…
nested
var out = '';
for (var i = 1; i <= 100; i++) {
  if (i % 3 == 0 && i % 5 == 0) {
    out = 'FizzBuzz';
  } else if (i % 3 == 0) {
    out = 'Fizz';
  } else if (i % 5 == 0) {
    out = 'Buzz';
  } else {
    out = i;
  }
}
pending…
cached results
var out = '',
  mod3 = 0,
  mod5 = 0;
for (var i = 1; i <= 100; i++) {
  mod3 = i % 3 == 0;
  mod5 = i % 5 == 0;

  if (mod3 && mod5) {
    out = 'FizzBuzz';
  } else if (mod3) {
    out = 'Fizz';
  } else if (mod5) {
    out = 'Buzz';
  } else {
    out = i;
  }
}
pending…
opposite nested
for (var i = 1; i <= 100; i++) {
  if (i % 3 != 0 && i % 5 != 0) {
    out = i;
  } else if (i % 3 == 0 && i % 5 != 0) {
    out = 'Fizz';
  } else if (i % 5 == 0 && i % 3 != 0) {
    out = 'Buzz';
  } else {
    out = 'FizzBuzz';
  }
}
pending…
cached opposite nested
var out = '',
  mod3 = 0,
  mod5 = 0;
for (var i = 1; i <= 100; i++) {
  mod3 = i % 3 == 0;
  mod5 = i % 5 == 0;
  if (!mod3 && !mod5) {
    out = i;
  } else if (mod3 && mod5) {
    out = 'Fizz';
  } else if (mod5 && !mod3) {
    out = 'Buzz';
  } else {
    out = 'FizzBuzz';
  }
}
pending…
cached opposite nested
var out = '',
  mod3 = 0,
  mod5 = 0;
for (var i = 1; i <= 100; i++) {
  mod3 = i % 3 == 0;
  mod5 = i % 5 == 0;
  out = '';
  if (!mod3 && !mod5) {
    out = i;
  } else {
    if (mod3) {
      out = 'Fizz';
    }
    if (mod5) {
      out += 'Buzz';
    }
  }
}
pending…
one-liner
var out = '';
for (var i = 1; i <= 100; i++) {
  out = (i % 3) ?
    (i % 5 ? i : 'Buzz') : (i % 5 ? 'Fizz' : 'FizzBuzz');
}
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