DEV Community

James Moberg
James Moberg

Posted on

ColdFusion IsValid("regex") vs reFind() vs Java Regex Pattern

I was recently notified of a slow ColdFusion script on a project and, after some isolation, determined that it was due to a regex-based UDF that we used in place of Adobe's built-in function (BIF) for isValid("url"). We used a regex rule with isValid("regex") to validate only HTTP/S URLs with valid hostname TLDs and the application was validating ~1,1000 strings from a database and taking ~1.5 seconds to perform.

Brad Wood recently blogged about improving the performance of Lucee's QofQ and he noted that "Adobe CF re-compiles the regex every single time". After reading that, I wondered if the same thing was happening with isValid("regex"). I'm not sure if it does or not, but if I had to guess, I'd say 'it depends'. CF2021 performance appears to be notably better than past performance, so it's possible. I was more concerned with cross-platform performance on both Adobe ColdFusion and Lucee CFML platforms, comparing and determining the best/fastest approach. I wrote a Short, Self Contained, Correct (Compilable), Example (SSCCE) so I could compare multiple CFML engines using either CommandBox or TryCF.com.

As a result of using a cached java regex pattern, the webpage processing time when from the absurdly slow 1-2 seconds to a more efficient 150ms.

Here are some preliminary performance comparisons of isValid("regex") versus reFind() versus a cached java regex pattern.

CFTry w/1,000 iterations (2022-12-02)

Platform isValid reFind Java
CF2021 146 167 10
CF2018 1,975 864 97
CF2016 2,520 2,484 38
Lucee 3,530 176 31

What's interesting is that tests seemed to run faster after executing it a couple times on TryCF. The cached java pattern remained significantly faster in every test and Lucee's isValid() is much slower than using reFind.

CFTry w/1 iteration (2022-12-02)

Platform isValid reFind Java
CF2021 0 1 0
CF2018 2 1 0
CF2016 5 5 0
Lucee 4 1 0

SSCCE Code

https://gist.github.com/JamoCA/e8c2ab5815bb26db58ff4024a86c533c

Top comments (0)