switch

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = 0,
        b = 1,
        c = 0,
        d = -1;
    var arr = [0, 1, 0, -1];
   
    function switchGetVal(i) {
      switch (i) {
      case 0:
        return a;
      case 1:
        return b;
      case 2:
        return c;
      case 3:
        return d;
      default:
        return null;
      }
    }
   
    function arrayGetVal(i) {
      return arr[i];
    }
};
</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 access
var acc;
for (var i = 0; i < 1000; i++)
acc += switchGetVal(i & 3);
pending…
array access
var acc;
for (var i = 0; i < 1000; i++)
acc += arrayGetVal(i & 3);
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