Jason Knight
2 min readOct 24, 2022

--

Sorry to say your "case" argument holds water like a steel... sieve. I hear that's the best kind... of sieve.

Right out of the gate the use of an object is not more "efficient" and certainly not more maintainable, because you're creating more steps to the logic and execution, much less the overhead of function calls and object lookups! You are creating performance overhead, increasing complexity by moving the actual techniques to an entirely different spot in the code (which gets worse in real applications), and depending on where you place or scope the object you're either pissing on the namespace or increasing execution time each time this gets called.

But worst of all? It's MORE code! You've BS yourself and/or are trying to card stack your evidence by putting the arrow functions as single lines.

As written, the switch/case is 214 bytes, the object lookup is 244. Modified so that they are the same formatting of // instead of /* */, comment and closing on their own lines, and it's 22 lines and 248 bytes. (all byte sizes calculated using tabs)

It is more code, more complex, and slower executing because you're introducing function overhead.

Whoever told you that object mapping is faster or better than switch-case is packing your fudge. In a "bend over for the ***-****** cracker with no Vaseline" way. Much less the "so long" statement which is absurd when you actually wrote more code, and/or more lines of code if formatted the same way!

const cases = {
a : () => {
// case 1
b : () => {
// case 2
},
c : () => {
// case 3
},
d : () => {
// case 4
},
e : () => {
// case 5
}
};
if (cases.hasOwnProperty(x)) {
cases[x]();
} else {
// case 6
}

See, 22 lines… 248 bytes as tabs, 267 as spaces. It’s LONGER. More complex, and in no way, shape, or form is it going to be faster.

Especially since you’re doing the object lookup TWICE which is performance hell in JavaScript.

--

--

Jason Knight
Jason Knight

Written by Jason Knight

Accessibility and Efficiency Consultant, Web Developer, Musician, and just general pain in the arse

No responses yet