Switch-case vs. if/else

Benchmark created by James Padolsey on


Preparation HTML

<script>
  var v = 0 | Math.random() * 3 + 1; // 1, 2, or 3
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Switch-case
switch (v) {
case 1:
 break;
case 2:
 break;
case 3:
 break;
default:
 ;
}
ready
if/else
if (v === 1) {

} else if (v === 2) {

} else if (v === 3) {

} else {

}
ready

Revisions

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