Even number AND vs mod

JavaScript performance comparison

Revision 3 of this test case created by Max

Info

Felt like creating a quick test-case to see if there is a benefit to the refactor offered by https://github.com/angular/angular.js/pull/4359

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Binary AND (10000 iterations)
var status;
for (var i = 0; i < 10000; ++i) {
  status = (i & 1) == 0 ? 'even' : 'odd';
}
pending…
Modulo 2 (10000 iterations)
var status;
for (var i = 0; i < 10000; ++i) {
  status = (i % 2) == 0 ? 'even' : 'odd';
}
pending…
Modulo 2 without equality test (10000 iterations)
var status;
for (var i = 0; i < 10000; ++i) {
  status = (i % 2) ? 'odd' : 'even';
}
pending…
Binary AND without equality test (10000 iterations)
var status;
for (var i = 0; i < 10000; ++i) {
  status = (i & 1) ? 'odd' : 'even';
}
pending…
Logical NOT with extra variable
var status;
var even = true;
for (var i = 0; i < 10000; ++i) {
  status = even ? 'even' : 'odd';
  even = !even; // prepare the next iteration
}
pending…
Equality
var status;
for (var i = 0; i < 10000; ++i) {
  status = (status == "even") ? 'odd' : 'even'; // the initial value is undefined
}
pending…
Bitwise NOT
var status;
var even = true;
for (var i = 0; i < 10000; ++i) {
  status = even ? 'even' : 'odd';
  even = ~even; // prepare the next iteration
}
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