If-else vs. switch vs. lookup table (v83)

Revision 83 of this benchmark created by Mark on


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 = {
      "pizza": function() {
        return "food";
      },
      "house": function() {
        return "building";
      },
      "table": function() {
        return "furniture";
      },
      "car": function() {
        return "driving";
      },
      "water": function() {
        return "drink";
      },
      "air":  function() {
        return "nothing";
      }
    };
    
    x = 'hello';

Test runner

Ready to run.

Testing in
TestOps/sec
if else
var stuff = 'water';
if(stuff === "pizza") {
  x += "food";
} else if (stuff === "house") {
  x += "building";
} else if (stuff === "table") {
  x += "furniture";
} else if (stuff === "car") {
  x += "driving";
} else if (stuff === "water") {
  x += "drink";
} else if (stuff === "air") {
  x += "nothing";
}
ready
switch
var stuff = 'water';  
switch (stuff) {
  case "pizza":
    x += "food";
  break;

  case "house":
    x += "building";
  break;

  case "table":
    x += "furniture";
  break;

  case "car":
    x += "driving";
  break;

  case "water":
    x += "drink";
  break;
 
  case "air":
    x += "nothing";
  break;
}
 
ready
lookup table
x += lookupTable["water"]();
 
ready

Revisions

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