switch vs object

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function getByObject(key) {
      return {
        'a': 1,
        'b': 2,
        'c': 3
      }[key];
    }
   
    var obj = {
    'a' : 1,
    'b' : 2,
    'c' : 3
    };
   
    function getByPredefObject(key){
    return obj[key];
    }
   
    function getBySwitch(key) {
      switch (key) {
        case 'a':
          return 1;
        case 'b':
          return 2;
        case 'c':
          return 3;
      }
    }
};
</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
get by object
getByObject('b');
pending…
get by switch
getBySwitch('b');
pending…
getByPredefObject
getByPredefObject('b');
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