Ternary vs Switch vs If (v2)

Revision 2 of this benchmark created on


Description

Testing which is faster, ternary, switch, if, if else if, or if else if else.

Setup

var a = 3,
        b = 2,
        c = 3,
        t = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Terary
t = (a == 1 ? 1 : a == 2 ? 2 : 3);

a = b;
b = c;
c = t;
ready
Switch
switch (a) {
case 1:
  t = 1;
  break;
case 2:
  t = 2;
  break;
case 3:
  t = 3;
  break;
}

a = b;
b = c;
c = t;
ready
If
if (a == 1) t = 1;
if (a == 2) t = 2;
if (a == 3) t = 3;

a = b;
b = c;
c = t;
ready
if else if
if (a == 1) t = 1;
else if (a == 2) t = 2;
else if (a == 3) t = 3;

a = b;
b = c;
c = t;
ready
if else if else
if (a == 1) t = 1;
else if (a == 2) t = 2;
else t = 3;

a = b;
b = c;
c = t;
ready

Revisions

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