Rip's Domain

CFSWITCH vs. Structure?

Posted in ColdFusion by rip747 on April 9, 2007

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 />
<cfswitch expression=”#num#”>
<cfcase value=”0″>
first
</cfcase>
<cfcase value=”3″>
second
</cfcase>
<cfcase value=”5″>
third
</cfcase>
</cfswitch>

And now my propsed example just use a simple structure:

<cfscript>
num = 3;
s = StructNew();
s[“0”] = “first”;
s[“3”] = “second”;
s[“5”] = “third”;
if(StructKeyExists(s, num))
{
writeoutput(s[num])
}
</cfscript>

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.

5 Responses

Subscribe to comments with RSS.

  1. Sam Farmer said, on April 9, 2007 at 9:43 pm

    For the example you show then yes a structure is better but I think Dave was doing that just to explain what he was doing.

    It is only when you are using a a switch/case with cfscript that you need to use break which is similar to other languages like JavaScript, Java, etc.

  2. rip747 said, on April 9, 2007 at 10:04 pm

    @sam,

    I agree. He was doing a very basic example to get his point across.

    I was using his example to propose my little trick since it was so simple and perfect.

  3. Derek P. said, on April 9, 2007 at 10:16 pm

    couldn’t you just have it fire off methods within your struct to simulate where the code would be in a case statement? then just return the key’s value.

  4. Sammy Larbi said, on April 10, 2007 at 7:03 am

    That’s a neat little trick that reminds me of using

    [cfif listfindnocase(“Red Dog,Old Milwaukee”, beer)]
    don’t you mean “swill?”
    [/cfif]

    in place of a long list of OR conditions.

    But, I wonder if it retains its usefulness in a more realistic example, and also if it shows its intent clearly enough.

    Do you have a better example?

    “We all know that structures are indexed and, because of this, faster than doing condition logic.”

    Does one follow the other?

  5. rip747 said, on April 10, 2007 at 8:58 am

    @derek

    don’t know if you could do that. if you have the time, why don’t you try it out and post it to your blog. let’s see if it would work.

    @sammy

    like i stated, this method could be used as a replacement in very simple logic. i don’t think you could use this method in more complex examples, but i could be wrong.

    9 times out of 10 index does make thing faster. at least that’s what i hope or my statement is wrong🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: