DEV Community

Discussion on: Alternative to if / else and switch: object literals in JavaScript

Collapse
 
ryanatk profile image
ryan atkinson

Back in the days of JSPerf, the object literal lookup was always faster. The difference grows larger as you add more lines of "if" checks.

It's also a helpful & popular pattern to declare your string constants in an all-caps "constant" name (so const passwords would be const PASSWORDS) to consolidate (as opposed to mixing them throughout lines of an if/else statement) and make them easily recognizable. Then the lookup would read PASSWORDS[password.toLowerCase()]. Definitely not a "rule", but helpful to the reader.