If-else with indexOf vs. switch vs. lookup table (v45)

Revision 45 of this benchmark created on


Description

added indexOf to if-else

Setup

function ifElseTest(stuff) {
      if(stuff.indexOf("pizza") != -1) {
        return "food";
      } else if (stuff.indexOf("house") != -1) {
        return "building";
      } else if (stuff.indexOf("table") != -1) {
        return "furniture";
      } else if (stuff.indexOf("car") != -1) {
        return "driving";
      } else if (stuff.indexOf("water") != -1) {
        return "drink";
      } else if (stuff.indexOf("air") != -1) {
        return "nothing";
      }
    };
    
    function switchTest(stuff) {
      switch (stuff) {
        case "pizza":
          return "food";
        break;
    
        case "house":
          return "building";
        break;
    
        case "table":
          return "furniture";
        break;
    
        case "car":
          return "driving";
        break;
    
        case "water":
          return "drink";
        break;
     
        case "air":
          return "nothing";
        break;
      }
    };
    
    var lookup = {
      "pizza": "food",
      "house": "building",
      "table": "furniture",
      "car": "driving",
      "water": "drink",
      "air": "nothing"
    };
    function lookupTable(value) { 
        return lookup[value];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
if else
ifElseTest("pizza");
ifElseTest("water");
ifElseTest("air");
ready
switch
switchTest("pizza");
switchTest("water");
switchTest("air");
ready
lookup table
lookupTable("pizza");
lookupTable("water");
lookupTable("air");
ready

Revisions

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