Switch vs Object

JavaScript performance comparison

Test case created by Paul Massey

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Switch
function getProgress(name) {
  var progress;
  switch (name) {
    case 'one':
      {
        progress = 20;
        break;
      }
    case 'two':
      {
        progress = 40;
        break;
      }
    case 'three':
      {
        progress = 60;
        break;
      }
    case 'four':
      {
        progress = 60;
        break;
      }
    case 'five':
      {
        progress = 60;
        break;
      }
    case 'six':
      {
        progress = 80;
        break;
      }
    case 'seven':
      {
        progress = 90;
        break;
      }
    case 'eight':
      {
        progress = 100;
        break;
      }
    default:
      {
        progress = 0;
      }
  }
  return progress;
}

getProgress('one');
getProgress('two');
getProgress('three');
getProgress('four');
getProgress('five');
getProgress('six');
getProgress('seven');
getProgress('eight');
getProgress('whoops');
pending…
Object
function getProgress(name) {
  return {
    one: 20,
    two: 40,
    three: 60,
    four: 60,
    five: 60,
    six: 80,
    seven: 90,
    eight: 100
  }[name] || 0;
}

getProgress('one');
getProgress('two');
getProgress('three');
getProgress('four');
getProgress('five');
getProgress('six');
getProgress('seven');
getProgress('eight');
getProgress('whoops');
pending…

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

Compare results of other browsers

0 Comments