switch vs object literal vs module (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  // alternatives to switch
  var switchObj = {

    alpha: function() {
      return true;
    },
    _default: function() {
      return false;
    }

  };

  // called by switch 
  var alpha = function() {
      return true;
      };
  var beta = function() {
      return false;
      };

  // test var
  var foo = "alpha";
  var foo2 = "lolwut";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
switch
switch (foo) {

case "alpha":

  alpha();

  break;

default:

  // something to default to
  beta();
  break;

}

switch (foo2) {

case "alpha":

  alpha();

  break;

default:

  // something to default to
  beta();
  break;

}
ready
object literal
(switchObj.hasOwnProperty(foo) && switchObj[foo] || switchObj['_default'])();
(switchObj.hasOwnProperty(foo2) && switchObj[foo2] || switchObj['_default'])();
ready

Revisions

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