Boolean vs true false vs 1 0

JavaScript performance comparison

Revision 8 of this test case created by pkoretic

Info

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

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var booleanTrue;
    var booleanFalse;
    var trueTrue;
    var falseFalse;
    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()
 booleanTrue = Boolean(1);
 booleanFalse  = Boolean(0);
pending…
Boolean value
trueTrue = true;
falseFalse = false;
pending…
integers
numTrue = 5;
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