Boolean check: implicit VS ===true VS integer(!=0)

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var booleans = [];
    var ints = [];
    loops = 100
    for (var i = 0; i < loops; i++) {
      booleans.push((i & 1) != 0);
      ints.push(i & 1);
    }
    var sum = 0;
};

Benchmark.prototype.teardown = function() {
    print("sum=" + sum);
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Implicit boolean
// async test
for (var i = 0; i < loops; i++)
  if (booleans[i]) sum++;
pending…
if
// async test
for (var i = 0; i < loops; i++)
  if (booleans[i] === true) sum++;
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