Ternary vs Switch vs If (v18)

Revision 18 of this benchmark created on


Description

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

Preparation HTML

<script>
var a = 1,
    b = 2,
    c = 3,
    t = 0;
</script>

Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
Terary
t = (a == 1 ? 1 : a == 2 ? 2 : 3);
ready
Switch
switch (a) {
case 1:
  t = 1;
  break;
case 2:
  t = 2;
  break;
case 3:
  t = 3;
  break;
}
ready
If
if (a == 1) t = 1;
if (a == 2) t = 2;
if (a == 3) t = 3;
ready
if else if
if (a == 1) t = 1;
else if (a == 2) t = 2;
else if (a == 3) t = 3;
ready
if else if else
if (a == 1) t = 1;
else if (a == 2) t = 2;
else t = 3;
ready

Revisions

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