Ternary operators vs if

JavaScript performance comparison

Test case created by Klaas Cuvelier

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var s = 'test', 
      o = { a: 1, b: 'test', c: { a: 1 }, 'test-test': []};
  

};
</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
ternary with simple string - reassign same value
s = true ? s : 'change';
pending…
ternary with simple string - assign new value
s = false ? s : 'change';
pending…
if with simple string - no change
if (false) {
    s = 'change';
}
pending…
if with simple string - change value
if (true) {
    s = 'change';
}
pending…
ternary with object - reassign same value
o = true ? o : {};
pending…
ternary with object - assign new value
o = false ? o : {};
pending…
if with object - no change
if (false) {
  o = {};
}
pending…
if with object - assign new value
if (true) {
    o = {}
}
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