Switch vs Hash vs Others (v65)

Revision 65 of this benchmark created on


Setup

var index = 0;
  var foo = 0;
  var cached = {};
  
  function myFunction0() {
    foo++;
  }
  
  function myFunction1() {
    foo++;
  }
  
  function myFunction2() {
    foo++;
  }
  
  function myFunction3() {
    foo++;
  }
  
  function myFunction4() {
    foo++;
  }
  
  function myFunction5() {
    foo++;
  }
  
  function myFunction6() {
    foo++;
  }
  
  function myFunction7() {
    foo++;
  }
  
  function myFunction8() {
    foo++;
  }
  
  function myFunction9() {
    foo++;
  }
  
  var cases = [];
  cases[0] = myFunction0;
  cases[1] = myFunction1;
  cases[2] = myFunction2;
  cases[3] = myFunction3;
  cases[4] = myFunction4;
  cases[5] = myFunction5;
  cases[6] = myFunction6;
  cases[7] = myFunction7;
  cases[8] = myFunction8;
  cases[9] = myFunction9;
  
  
  var cases_object = {
  '0': myFunction0,
  '1': myFunction1,
  '2': myFunction2,
  '3': myFunction3,
  '4': myFunction4,
  '5': myFunction5,
  '6': myFunction6,
  '7': myFunction7,
  '8': myFunction8,
  '9': myFunction9
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Object hash
for (index = 0; index < 10; index++) {
  cases_object[index]();
}
ready
Object hash cached
for (index = 0; index < 10; index++) {
  cached = cases_object[index];
  cached();
}
ready
Switch string
for (index = 0; index < 10; index++) {
  switch (index) {
    case '0':
      myFunction0();
      break;
    case '1':
      myFunction1();
      break;
    case '2':
      myFunction2();
      break;
    case '3':
      myFunction3();
      break;
    case '4':
      myFunction4();
      break;
    case '5':
      myFunction5();
      break;
    case '6':
      myFunction6();
      break;
    case '7':
      myFunction7();
      break;
    case '8':
      myFunction8();
      break;
    case '9':
      myFunction9();
      break;
  }
}
ready

Revisions

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