if vs switch vs object

JavaScript performance comparison

Revision 2 of this test case created by

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
if
var a = 10;
if (a == 1) {
console.log(a);
} else if (a == 2) {
console.log(a);
} else if (a == 3) {
console.log(a);
} else if (a == 4) {
console.log(a);
} else if (a == 5) {
console.log(a);
} else if (a == 6) {
console.log(a);
} else if (a == 7) {
console.log(a);
} else if (a == 8) {
console.log(a);
} else if (a == 9) {
console.log(a);
} else if (a == 10) {
console.log(a);
} else  {
console.log(-1);
}
pending…
switch
var a = 10;

switch (a) {
case 1:
  console.log(a);
  break;
case 2:
  console.log(a);
  break;
case 3:
  console.log(a);
  break;
case 4:
  console.log(a);
  break;
case 5:
  console.log(a);
  break;
case 6:
  console.log(a);
  break;
case 7:
  console.log(a);
  break;
case 8:
  console.log(a);
  break;
case 9:
  console.log(a);
  break;
case 10:
  console.log(a);
  break;
default:
  console.log(-1);
}
pending…
object
var a = 10;

var actions = {
 0: function(a){ console.log(a); },
 1: function(a){ console.log(a); },
 2: function(a){ console.log(a); },
 3: function(a){ console.log(a); },
 4: function(a){ console.log(a); },
 5: function(a){ console.log(a); },
 6: function(a){ console.log(a); },
 7: function(a){ console.log(a); },
 8: function(a){ console.log(a); },
 9: function(a){ console.log(a); },
10: function(a){ console.log(a); }
}

(actions[a] || function(){ console.log(-1); })(a);
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