Switch-case vs. if/else

JavaScript performance comparison

Test case created by James Padolsey

Preparation code

<script>
  var v = 0 | Math.random() * 3 + 1; // 1, 2, or 3
</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-case
switch (v) {
case 1:
 break;
case 2:
 break;
case 3:
 break;
default:
 ;
}
pending…
if/else
if (v === 1) {

} else if (v === 2) {

} else if (v === 3) {

} else {

}
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