Switch vs Object

JavaScript performance comparison

Test case created by RayFranco

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var foo, test = 5,
      case1 = function() {
      foo = 1
      },
      case2 = function() {
      foo = 2
      },
      case3 = function() {
      foo = 3
      },
      case4 = function() {
      foo = 4
      },
      case5 = function() {
      foo = 5
      };

};
</script>

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 statement
(function(i) {
  var bar;

  switch (i) {
  case 1:
    bar = 'red';
    break;
  case 2:
    bar = 'blue';
    break;
  case 3:
    bar = 'yellow';
    break;
  case 4:
    bar = 'green';
    break;
  case 5:
    bar = 'black';
    break;
  }
  return bar;
})(test);
pending…
Object notation
(function(i) {
  return {
    1: 'red',
    2: 'blue',
    3: 'yellow',
    4: 'green',
    5: 'black'
  }[i];
})(test);
pending…

Compare results of other browsers

Revisions

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

0 Comments