Comparisons with CFIF

Jan 10, 2000
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 MethodFastest (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
*All tests done on CF 4.5 - results may vary


The simplest type of comparison is also the default. This uses no functions for comparison, relying instead on the basic power of the CFIF tag.

<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.


Logically, using a function to do a comparison inside a CFIF should add to overhead rather than reduce it. When a function is run, ALL of the variables within it are evaluated and then the function is executed. Even after that, the CFIF tag has to look at the result as TRUE or FALSE like any comparison. This looks like more operations and it is, but it's still faster. A function simply takes a variable and uses it with no checking of what the data type is. This can lead to more mixed data type errors, but also leads to a faster execution. In addition, if done right, the result of a function should be a Boolean, which is the fastest comparison operation within a CFIF.

Not Compare / Not CompareNoCase

This is the best and fastest type of comparison function around. It takes two values and says if they are exactly alike. There is both a case-sensitive and non-case-sensitive version of this function. The function will return a -1 or a +1 if one of the variables is less or more than the other. Only if they are exactly alike will it return a 0. In Boolean terms, a 0 is FALSE. To make it TRUE and useful within a CFIF, we have to use a NOT logical modifier before the function. This says, NOT FALSE, which is the same as true. Because of this, it is not as intuitive for people to look at the code and understand it, which is, for me, its only drawback.

<CFIF Not Compare(Variable, 'value')>
<CFIF Not CompareNoCase(Variable, 'value')>

Find / FindNoCase

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')>

REFind / REFindNoCase

I've included these functions, as they will also allow exact comparisons between two values and a whole lot more. Personally, I'd never use them for simple comparisons, as they have a bit more overhead to go with the extra power.
<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. :)

Privacy | FAQ | Site Map | About | Guidelines | Contact | Advertising | What is ColdFusion?
House of Fusion | ColdFusion Jobs | Blog of Fusion | AHP Hosting