Boolean vs true false vs 1 0

JavaScript performance comparison

Revision 7 of this test case created by Ian Nartowicz

Info

Which works faster (is there a difference), using Boolean(), true/false, !!, or 1/0?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var booleanTrue = Boolean(1);
    var booleanFalse = Boolean(0);
    var trueTrue = true;
    var falseFalse = false;
    var numTrue = 5;
    var numFalse = 0;
   
};
</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
Boolean()
if ( booleanTrue ) {}
if ( booleanFalse ) {}
pending…
!!
if ( !!numTrue ) {}
if ( !!numFalse ) {}
pending…
true/false
if ( trueTrue) {}
if ( falseFalse) {}
pending…
One Zero
if ( numTrue) {}
if ( numFalse ) {}
pending…
Explicit
if ( numTrue == 5) {}
if ( numFalse == 0) {}
pending…
Explicit equals equals
if ( numTrue === 5) {}
if ( numFalse === 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