switch vs object literal vs module (v5)

Revision 5 of this benchmark created by Tobias Schneider 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";

  var args = [];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
switch
switch (foo) {

case "alpha":

 alpha();

 break;

default:

 // something to default to
 break;

}
ready
object literal
switchObj[foo].apply(null, args);
ready
module
switchModule[foo].apply(null, args);
ready

Revisions

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