If-else vs. switch vs. lookup table juan (v59)

Revision 59 of this benchmark created by Bacher on


Description

one function for LUtable

Setup

function ifElseTest(stuff) {
      if (stuff === "pizza") {
        return "food";
      } else if (stuff === "house") {
        return "building";
      } else if (stuff === "table") {
        return "furniture";
      } else if (stuff === "car") {
        return "driving";
      } else if (stuff === "water") {
        return "drink";
      } else if (stuff === "air") {
        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 lookupTable = function(blud) {
      var table = {
        "pizza": "food",
        "house": "building",
        "table": "furniture",
        "car": "driving",
        "water": "drink",
        "air": "nothing"
      };
      return table[blud];
    };
    
    var table = {
      "pizza": "food",
      "house": "building",
      "table": "furniture",
      "car": "driving",
      "water": "drink",
      "air": "nothing"
    };
    
    var lookupTableCached = function(blud) {
      return table[blud];
    };

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
lookup cached table
lookupTableCached("pizza");
lookupTableCached("water");
lookupTableCached("air");
ready

Revisions

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