Pete Freitag Pete Freitag

Performance Tip: Upper Case vs Lower Case

Published on November 05, 2004
By Pete Freitag

Many people think that converting a given string to upper case and converting to lower case will perform the same. While they probably do use a very similar algorithm, the fact the most text is lower case makes converting to lower cause a more efficient option for most strings. The only time they will perform the same is if half the text is upper case, and half is lower case. Sometimes you have a choice on which case you can convert to, for instance:

SELECT firstname 
FROM users
WHERE UPPER( firstname ) = <cfqueryparam value="#UCase(url.firstname)#" cfsqltype="cf_sqlvarchar">

In this example (searching for a firstname) it doesn't really make sense to convert to upper case, assuming the first name is stored something like Pete, and the user input was probably either Pete or pete. We can save some resources by converting to lower case instead.

To understand the differences lets take a look at how one might implement an algorithm to convert a string to upper case.

loop over each character {
  if (character code is in the range of a lower case character) {
      character code = character code + offset;

So if a character is uppercase (not in the range of the lower case character codes) then all that happens is a simple numerical compersion, and then move on to the next character. If the character is lower case then we still do the numerical compersion, and also perform an addition (which includes two reads, and a mathematical operation), and write a value to memory.

So next time you have a choice to either convert a string to upper case or lower case stop for a second and think about the string. In most cases it will make sense to convert to lower case. In some cases such as with acronyms it makes more sense to use upper case.

The performance gains from this probably isn't something worth checking all your existing code for, but is something to keep in mind for the future.

Performance Tip: Upper Case vs Lower Case was first published on November 05, 2004.


The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.

Try Fixinator

The weekly newsletter for the CFML Community


Another thing to note is that case conversions in UTF-8 have the ability to change the stored byte size of the converted string, sometimes resulting in buffer reallocation.
by Finn on 11/06/2004 at 5:44:00 PM UTC
If you're talking that level of performance wouldn't

WHERE UPPER( firstname ) = UPPER(<cfqueryparam value="#url.firstname#" cfsqltype="cf_sqlvarchar">)

also be faster? I'm assuming that the DB upper is performed in machine language and CF is interpreted, or at least byte code. I always try and push operations to the DB when possible.
by Dave on 08/13/2007 at 4:57:31 PM UTC