switch vs object literal vs module (v11)

Revision 11 of this benchmark created on


Preparation HTML

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

    alpha: function() {

      return true;
    }

  },
      switchModule = (function() {

      return {

        alpha: function() {

          return true;
        }

      };

    })();

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

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

Test runner

Ready to run.

Testing in
TestOps/sec
switch
switch (foo) {
case "beta":
  break;
case "alpha":

  switchObj.alpha();

  break;

default:

  // something to default to
  break;

}
ready
object literal check that it exists
if (foo in switchObj) switchObj[foo]();
ready
module
if (foo in switchModule) switchModule[foo]();
ready
Object without check
switchModule[foo]();
ready
switch without default
switch (foo) {
case "beta":
  break;
case "alpha":

  switchObj.alpha();

  break;
}
ready

Revisions

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