If your "do somethings" share a similar form with different values, you can put the values into a map and use the string as a key. For example's sake, imagine you have to process many numbers with different units of length and you want to convert them all to meters:
(Abbreviations (like "km") and international spellings (like "kilometres") omitted for brevity.) You can prepare that map ahead of time to avoid creation overhead. Now, given a variable
If your "do somethings" do not share common code you can still use a map, but it will be a map of functions:
This is a bit of a silly example but I hope it explains the basic idea. The actions could equally be XML tags, or names of CPU instructions in a virtual machine, or names of products that have special shipping requirements, or whatever. Once you've got your
A map isn't the only way to do this kind of thing. Your original if/else example can be optimized easily by nesting the ifs inside additional ifs that are designed to quickly eliminate most of the candidate strings. The criteria you branch on will depend on the exact strings you're working with. It could be the length of the string, or the first letter, or a couple of the most distinguishing letters:
Or:
You can nest these kind of tests inside one another to whatever degree is appropriate to sift through the particular strings you're working with. This is a sort of unrolled binary search, although the criteria you branch on do not have to divide the candidate strings into exactly two groups. Also, when you use an if/elseif like this, it's often worth arranging the strings in descending order of frequency. I.e., test the ones that happen most, first. If there are just a couple of strings that make up the majority of the data, pull them to the top, and even put them outside of any pre-tests based on length or first letter. You'll have to decide whether it's worth doing these things: if you take these techniques to the extreme, you might be able to squeeze tiny additional performance benefits, but it will sacrifice readability and maintainability. P.S. I don't know JavaScript well enough to know exactly how these techniques
will perform but I've done similar things in Java. In Java the map approach is unbeatable when the "do somethings" require different values but can use the same code. In a different program, I needed to P.P.S. There's an approach I skipped above but I'll include it for completeness: if your strings will rarely need changing, you can use a perfect hash function. There are utility programs that design these functions for you: just supply them with a list of all your strings. A perfect hash function will calculate an integer hashcode from a string, and guarantee that no two strings from your set have the same hashcode. Then you can use the integer hashcode for lookup of the action in an array. It's helpful for things like parsing keywords of programming languages. It can be faster in a language that is closer to the metal, but in JavaScript I suspect it will not be worth it. I'm mentioning it just in case. |