switch-vs-if-else

JavaScript performance comparison

Test case created by juan

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var x= "";
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
switch
var day=new Date().getDay();
switch (day)
{
case 0:
  x="Today it's Sunday";
  break;
case 1:
  x="Today it's Monday";
  break;
case 2:
  x="Today it's Tuesday";
  break;
case 3:
  x="Today it's Wednesday";
  break;
case 4:
  x="Today it's Thursday";
  break;
case 5:
  x="Today it's Friday";
  break;
case 6:
  x="Today it's Saturday";
  break;
}
pending…
if-else
var day=new Date().getDay();
if(day === 0){
  x="Today it's Sunday";
} else if(day === 1){
x="Today it's Monday";
} else if(day === 2){
 x="Today it's Tuesday";
} else if(day === 3){
  x="Today it's Wednesday";
} else if(day === 4){
  x="Today it's Thursday";
} else if(day === 5){
  x="Today it's Friday";
} else if(day === 6){
   x="Today it's Saturday";
}  
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

Add a comment