Ternary vs if/else

JavaScript performance comparison

Revision 3 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = true,
    b;

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
strict equal
if (a === true) {
  b = true;
} else {
  b = false;
}

if (a === false) {
  b = true;
} else {
  b = false;
}
pending…
ternary strict equals
b = (a === true) ? true : false;

b = (a === false) ? true : false;
pending…
normal equal
if (a == true) {
  b = true;
} else {
  b = false;
}

if (a == false) {
  b = true;
} else {
  b = false;
}
pending…
ternary equal
b = (a == true) ? true : false;

b = (a == false) ? true : false;
pending…
truthy/falsy
if (a) {
  b = true;
} else {
  b = false;
}

if (!a) {
  b = true;
} else {
  b = false;
}
pending…
ternary truthy/falsy
b = (a) ? true : false;
b = (!a) ? true : 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.

0 Comments