if/else vs arrays vs switch vs ternary (v3)

Revision 3 of this benchmark created on


Description

checks performance of the many types of case iterators in javascript.

Setup

var x;

Test runner

Ready to run.

Testing in
TestOps/sec
switch
switch (3) {
case 1:
  x = 10;
  break;
case 2:
  x = 18448;
  break;
case 3:
  x = 188888;
  break;
case 4:
  x = 166;
  break;
case 5:
  x = 1516;
  break;
case 6:
  x = 11105;
  break;
case 7:
  x = 1215;
  break;
case 8:
  x = 1116;
  break;
}
ready
if else
if (3 == 1) x = 10;
else if (3 == 2) x = 18448;
else if (3 == 3) x = 188888;
else if (3 == 4) x = 166;
else if (3 == 5) x = 1516;
else if (3 == 6) x = 11105;
else if (3 == 7) x = 1215;
else if (3 == 8) x = 1116;
ready
indexed arrays
var testArray = [, 10, 18448, 188888, 166, 1516, 11105, 1215, 1116];
x = testArray[3];
ready
object
var testObject = {
  1: 10,
  2: 18448,
  3: 188888,
  4: 166,
  5: 1516,
  6: 11105,
  7: 1215,
  8: 1116
};
x = testObject[3];
ready
ternary
x = 3 == 1 ? 10 : 3 == 2 ? 18448 : 3 == 3 ? 188888 : 3 == 4 ? 166 : 3 == 5 ? 1516 : 3 == 6 ? 11105 : 3 == 7 ? 1215 : 3 == 8 ? 1116 : 15;
ready
Boolean
x = 3 == 1 && 10 || 3 == 2 && 18448 || 3 == 3 && 188888 || 3 == 4 && 166 || 3 == 5 && 1516 || 3 == 6 && 11105 || 3 == 7 && 1215 || 3 == 8 && 1116 || 15;
ready

Revisions

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