Bool vs doublebang

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var sideEffect = 0;
   
    function doubleBang(x) {
      sideEffect += 1;
      return !!x;
    }
   
    function booleanCast(x) {
      sideEffect += 1;
      return Boolean(x);
    }
   
    function bitwise(x) {
      sideEffect += 1;
      return ~~x;
    }
   
    function equals(x) {
      sideEffect += 1;
      return x == true;
    }
   
    function returns(x) {
      sideEffect += 1;
      return x;
    }
};
</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
Double bang
doubleBang(1) && doubleBang(0);
pending…
Boolean typecast
booleanCast(1) && booleanCast(0);
pending…
Bitwise
bitwise(1) && bitwise(0);
pending…
Double equals
equals(1) && equals(0);
pending…
Just the variable
returns(1) && returns(0);
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