switch vs object literal vs module (v3)

Revision 3 of this benchmark created by Rick Waldron 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[foo] || switchObj._default)();
(switchObj[foo2] || switchObj._default)();
ready

Revisions

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