Andy Jarrett // Code. Develop. Create.

reReplace or replace: That is the Question

reReplace or replace: That is the Question

I had to investigate this, as these sorts of performance comparisons always grab my interest. It all started with a post in alt.comp.lang.coldfusion, where Drew asked: "I'm trying to take this value 0.85 and turn it into 85, or 1.35 and turn that into 135."

There were several suggestions, but two stood out: using reReplace() and replace(). These prompted me to test which one would perform better. The results were quite interesting, especially when tested on **ColdFusion 4.5**—and those were shocking! Below are the tests.

Test 1: Using reReplace()


<cfset tickBegin = GetTickCount()>

<cfset a = "01234567.89">

<cfloop from="1" to="100000" index="b">
    <cfset c = reReplace(a,'[^0-9]','','ALL')>
</cfloop>

<cfset tickEnd = GetTickCount()>
<cfset loopTime = tickEnd - tickBegin>

<cfoutput>#loopTime#</cfoutput>

Test 2: Using replace()


<cfset tickBegin = GetTickCount()>

<cfset a = "01234567.89">

<cfloop from="1" to="100000" index="b">
    <cfset c = replace(a,'.','','ALL')>
</cfloop>

<cfset tickEnd = GetTickCount()>
<cfset loopTime = tickEnd - tickBegin>

<cfoutput>#loopTime#</cfoutput>

Performance Comparison

Test 1: reReplace() Test 2: replace()
# CF4.5 CFMX CF4.5 CFMX
1. 5939 2223 4627 190
2. 5887 2163 4737 200
3. 5908 2153 4817 191

Conclusion

First thing—WOW! How did we ever manage to work efficiently with CF4.5?

As you can see, by avoiding the regex engine, the execution times in **ColdFusion MX** drop significantly—by almost **100%**. This proves that for simple string replacements, avoiding regex (using replace()) can provide massive performance improvements.

Would love to hear your thoughts—let me know if you've found similar results in your own testing!

I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee. Your support keeps this site running and the coffee brewing! ☕️