Boolean converting

JavaScript performance comparison

Revision 3 of this test case created by Jens Duttke

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var result,
        strTrue = "string",
        strFalse = "",
        numTrue = 1,
        numFalse = 0,
        nullFalse = null,
        undefFalse = undefined;
};
</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 negation
result = !!strTrue;
result = !!strFalse;
result = !!numTrue;
result = !!numFalse;
result = !!nullFalse;
result = !!undefFalse;
 
pending…
Boolean
result = new Boolean(strTrue);
result = new Boolean(strFalse);
result = new Boolean(numTrue);
result = new Boolean(numFalse);
result = new Boolean(nullFalse);
result = new Boolean(undefFalse);
pending…
Boolean (fixed)
result = Boolean(strTrue);
result = Boolean(strFalse);
result = Boolean(numTrue);
result = Boolean(numFalse);
result = Boolean(nullFalse);
result = Boolean(undefFalse);
pending…
Conditional operator
result = strTrue ? true : false;
result = strFalse ? true : false;
result = numTrue ? true : false;
result = numFalse ? true : false;
result = nullFalse ? true : false;
result = undefFalse ? true : false;
 
pending…
Conditional statement
if (strTrue) result = true; else result = false;
if (strFalse) result = true; else result = false;
if (numTrue) result = true; else result = false;
if (numFalse) result = true; else result = false;
if (nullFalse) result = true; else result = false;
if (undefFalse) result = true; else result = false;
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