Boolean vs true false vs 1 0

JavaScript performance comparison

Test case created by Kyle A. Matheny

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 = 1;
    var numFalse = 0;
    var exTrue = !!1;
    var exFalse = !!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 ( exTrue ) {}
if ( exFalse ) {}
pending…
true/false
if ( trueTrue) {}
if ( falseFalse) {}
pending…
One Zero
if ( numTrue) {}
if ( numFalse ) {}
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