If-else vs. switch vs. lookup table

JavaScript performance comparison

Revision 4 of this test case created by Jonatas Miguel

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function ifElseTest(stuff) {
      if(stuff === 1) {
        return "food";
      } else if (stuff === 2) {
        return "building";
      } else if (stuff === 3) {
        return "furniture";
      } else if (stuff === 4) {
        return "driving";
      } else if (stuff === 5) {
        return "drink";
      } else if (stuff === 6) {
        return "nothing";
      }
    };
   
    function switchTest(stuff) {
      switch (stuff) {
        case 1:
          return "food";
        break;
   
        case 2:
          return "building";
        break;
   
        case 3:
          return "furniture";
        break;
   
        case 4:
          return "driving";
        break;
   
        case 5:
          return "drink";
        break;
     
        case 6:
          return "nothing";
        break;
      }
    };
   
    var lookupTable = {
      1: function() {
        return "food";
      },
      2: function() {
        return "building";
      },
      3: function() {
        return "furniture";
      },
      4: function() {
        return "driving";
      },
      5: function() {
        return "drink";
      },
      6:  function() {
        return "nothing";
      }
    };
   
    var lookupArray = [
      undefined,
      function() {
        return "food";
      },
      function() {
        return "building";
      },
      function() {
        return "furniture";
      },
      function() {
        return "driving";
      },
      function() {
        return "drink";
      },
      function() {
        return "nothing";
      }
    ];
   
    var lookupTableString = {
      'a': function() {
        return "food";
      },
      'b': function() {
        return "building";
      },
      'c': function() {
        return "furniture";
      },
      'd': function() {
        return "driving";
      },
      'e': function() {
        return "drink";
      },
      'f':  function() {
        return "nothing";
      }
    };
};
</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
if else
ifElseTest(1);
ifElseTest(5);
ifElseTest(6);
pending…
switch
switchTest(1);
switchTest(5);
switchTest(6);
pending…
lookup table - access as string
lookupTable['1']();
lookupTable['5']();
lookupTable['6']();
pending…
lookup array
lookupArray[1]();
lookupArray[5]();
lookupArray[6]();
pending…
lookup table - integers
lookupTable[1]();
lookupTable[5]();
lookupTable[6]();
pending…
lookup table - string keys
lookupTableString['a']();
lookupTableString['e']();
lookupTableString['f']();
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. Here’s a list of current revisions for this page:

0 comments

Add a comment