benjamincharity
2/23/2016 - 4:35 PM

Using an object is more efficient than switch statements or nested if statements. https://github.com/loverajoel/jstips/blob/gh-pages/_posts/

Using an object is more efficient than switch statements or nested if statements. https://github.com/loverajoel/jstips/blob/gh-pages/_posts/en/2016-01-03-improve-nested-conditionals.md

var colorObj = {
  'black': printBlackBackground,
  'red': printRedBackground,
  'blue': printBlueBackground,
  'green': printGreenBackground,
  'yellow': printYellowBackground
};

if (color in colorObj) {
  colorObj[color]();
}

// if `color` === red, printRedBackground() would be called