CFSWITCH vs. Structure?
There has always been talks about the inefficiencies of using cfswitch in the ColdFusion world. Some say that it does have a big impact on performance. Dave Shuck makes a good point that you should always use cfbreak when using cfswitch since CF will continue to evaluate all cfcase blocks even after it finds a match.
Taking his example, I would like to propose another approach, one which doesn’t use cfswitch and just a plain structure to do the comparision. Let’s look at the Dave’s example:
<cfset num = 3 />
And now my propsed example just use a simple structure:
num = 3;
s = StructNew();
s[“0”] = “first”;
s[“3”] = “second”;
s[“5”] = “third”;
We all know that structures are indexed and, because of this, faster than doing condition logic. Obviously you can only do this in a case like the example above, for simple conditional lookups and not if the cfcase statement would be doing anything beyond that. In other words, you couldn’t use my example an do cfqueries or anything crazy. Another good thing about using this method for simple lookups is that you don’t need to worry, obviously, about using cfbreak.