There is no real performance gain is there? I assume apart from type safety the latter is better since I need less lines. What exactly is considered best practice in cases like these? Should I still cache the object eventhough its not needed?

So unless my if statements included:

if(elem.className == "something")

I personaly wouldnt bother caching.

At the other hand my brain is in conflict with coding style / consistency.

The "best practice" in "such" cases is to eliminate read(access) operations upon array/object.In your case you have 4 read operations for both two variants. - To avoid multiple read/access operations you should save the crucial element(reference) into a local variable- To avoid multiple if else statements - use switch operator instead(it should go faster)You should also consider the code readability and code simplicity. But if you need "the less lines" - I would suggest the following simple and scalable solution for your last example: