Ternary vs If-Else vs If (v14)

Revision 14 of this benchmark created by gion on


Description

Testing which is faster ternary, if-else, or just if.

Setup

var a,
      b,
      c,
      d,
      e = 3;
    a = Math.floor(Math.random() * 2);
    b = Math.floor(Math.random() * 2);
    c = Math.floor(Math.random() * 2);
    d = Math.floor(Math.random() * 2);

Teardown


    a = Math.floor(Math.random() * 2);
    b = Math.floor(Math.random() * 2);
    c = Math.floor(Math.random() * 2);
    d = Math.floor(Math.random() * 2);
  

Test runner

Ready to run.

Testing in
TestOps/sec
Ternary
if (a ? b === c : d) {
  e = 4;
}
return e;
ready
if-else
if (a) {
  if (b === c) {
    e = 4;
  }
} else if (d) {
  e = 4;
}
return e;
ready
If
if (a || d) {
  if (b === c || (!a && d)) {
    e = 4;
  }
}
return e;
ready
only ternary
e = (a ? b === c : d) ? 4 : e;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.