by Michael Dinowitz
There are a number of ways to compare two text strings inside a ColdFusion CFIF statement. The problem is, some are faster than others. Now you may think that comparing strings would be the same no matter what method is used. Unfortunately, this is not true. I could dream up a number of reasons for this but it really wouldn't matter. What does matter is results. For this reason, I'll be going over each of the comparison methods, giving you their average speeds and how to use them. If this information is used properly, then you should see a jump of 50% in your CFIF statements and hopefully some more speed in all your code.
Before we start, I'm going to assume you know the ins and outs of a CFIF statement. That being the case, what we're looking at here is either the Statement part of a CFIF or a single Clause of the statement.
|
<CFIF Statement>
<CFIF Clause [joiner] Clause> |
If you have any questions on the structure and proper use of a CFIF, you can check it out in the ColdFusion documentation, in any of the excellent ColdFusion books that have been written or you can wait till I release my paper on The Complete CFIF, which will be a slightly altered version of the Comparison chapter in the forthcoming book, Core ColdFusion.
| Comparison Method | Fastest (ms) | Average (ms) |
|---|---|---|
| Variable IS "Value" | .29 | .30 |
| NOT Compare(Variable, 'Value') | .10 | .11 |
| NOT CompareNoCase(Variable, 'Value') | .11 | .11 |
| Find(Variable, 'Value') | .10 | .11 |
| FindNoCase(Variable, 'Value') | .10 | .11 |
| REFind('Value', Variable) | .14 | .14 |
| REFindNoCase('Variable', Value) | .16 | .17 |
|
<CFIF Variable IS "Value">
|
While this statement works and works well, it's actually the slowest way to compare text values. I believe that this statement does a few evaluations before trying to do any sort of comparison. The first evaluation is to see if either value is a Boolean and the second is to see if either is a Numeric. If they are one of these data types, then the actual comparison goes rather fast. (This is because of the way ColdFusion processes Boolean and numbers vs. text values.) If not, then a standard text comparison is performed. I've come to this conclusion after a few tests:
|
<CFIF
Variable IS "True"> <!--- This is the text value TRUE --->
<CFIF Variable IS True> <!--- This is the Boolean value TRUE ---> |
|
<CFIF Variable IS "100"> <!--- This is the text value 100 --->
<CFIF Variable IS 100> <!--- This is the numeric value 100 ---> |
Both of these groups operated at exactly the same speeds, even though one was treating a value as text and the other was treating it as a Boolean or a number. This says that some other operation is going on than plain text comparison.
|
<CFIF Not Compare(Variable, 'value')>
<CFIF Not CompareNoCase(Variable, 'value')> |
These functions are a lot more intuitive, but also more problematic. The functions will take two values and compare them. If the first value is in the second value, the function will return the numeric position of the match. The problem is, this can be ANYWHERE in the second value. This means that a simple comparison that looks like it should logically work can fail.
|
<CFIF FindNoCase('admin', 'useradmin')>
<CFIF FindNoCase('admin', 'administrator')> |
The first example will return a 'TRUE' result to a CFIF that uses it, but the actual result is 5. The second will also return a 'TRUE' result and the value will be 1, but it's not an exact match for what we were expecting. With some fine control this can be used, but is too prone to problems for my taste.
|
<CFIF Find(Variable, 'value')>
<CFIF FindNoCase(Variable, 'value')> |
|
<CFIF REFind('value', Variable)>
<CFIF REFindNoCase('value, Variable)> |
My advice after all this is to go through all your code, look for any places that have a standard CFIF, and change it to one of the above functions. You'll save in speed and in the end, that's all that matters. To make your job a little easier, this Regular Expression will work in Studio to find every location that should be changed:
|
<CFIF[^>]+ IS [^>]+>
|
It might also be a smart idea to comment all the changes. It might save your life if you ever have to change it all back. :)
coral,turquoise jewelry, shell,gemstone jewelry, pearl bridal jewelry and wish pearls gifts in China.