Object vs regular switch

Benchmark created by Felix Geisendörfer on


Description

Regular vs. object switch.

Setup

var switchObject = {
      'alpha': function() {
        i = i + 1;
      },
      'beta': function() {
        i = i + 2;
      },
      '_default': function() {
        i = i + 3;
      }
    };
    
    var i = 0;
    var methods = ['alpha', 'beta', 'non-existing'];
    var method = methods[Math.floor(Math.random() * methods.length)];

Test runner

Ready to run.

Testing in
TestOps/sec
Object Switch
(switchObject[method] || switchObject._default)()
ready
Regular Switch
switch(method) {
  case 'alpha':
    i = i + 1;
    break;
  case 'beta':
    i = i + 2;
    break;
  default:
    i = i + 3;
    break;
}
ready

Revisions

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

  • Revision 1: published by Felix Geisendörfer on