AdobeStock_455007340

Checking For Empty Strings

Which is the faster way to check whether or not a string is empty, or ? The commonly held view is that it is the latter, but, after a heated debate today on the subject of optimization, I decided to test it for myself. The result? Almost no difference at all, in fact, I had to run the test thousands of times to even see a difference. Here is the code I used:



























Run it for yourself. In my tests they were about the same, Len() was usually slightly quicker, but almost insignificantly so. And it took 100,000 iterations to see results worthy of graphing. In other words, there are about the same so use whichever you prefer.

9 responses to “Checking For Empty Strings”

  1. Oliver Merk Avatar
    Oliver Merk

    Hi Ben,
    I found that on first run, Len(str) was *twice* as fast. On subsequent runs, they were almost equal (with Len(str) a little faster). Think I’ll stay in the "Len(str)" camp;)
    Cheers,
    Oliver
    PS How’s CFX_spell for MX coming?

  2. Bram Avatar
    Bram

    Hello,
    I have a fresh install of CF 6.1 (dev. edition).
    I copy pasted your code and got these results:
    IS NOT:937
    Len(): 624.7

  3. Bram Avatar
    Bram

    I’m sorry for my second comment,
    but the fact is I get very different
    results everytime I run the code …

  4. Calvin Avatar
    Calvin

    I got;
    IS NOT: 1562
    Len(): 1110

  5. Marcos Placoná Avatar
    Marcos Placoná

    I’ve tried using the IsDefined() function, and it was the slowest.
    And in majority the Len() was faster
    Regards,
    Marcos Placoná

  6. seancorfield Avatar
    seancorfield

    Interesting. I get very strange results. The "is not" code consistently takes 700ms on every run +/-5% but the "len()" code alternates between about 650ms and 2,000ms. So I took out the cfchart stuff and just output the timings and they were more consistent:
    is not: 650ms
    len(): 1,500ms
    This is CFMX6.1 on Tomcat with 1.4.1_01 JVM on a PowerBook G4 (800MHz, 512Mb RAM).

  7. Nathan Dintenfass Avatar
    Nathan Dintenfass

    Another thing to keep in mind is also the construct: len(trim(foo))
    That’s especially useful when testing for input because simply using len() or EQ "" will not work if the end user put in a space (or tab or other white space chars), and in most of those cases you still want to know if no "real" characters were entered.

  8. Nathan Dintenfass Avatar
    Nathan Dintenfass

    FWIW, I had much more consistent results than Sean on a virtually identical setup (G4/Tomcat/6.1/1.4.1) — the IS NOT and len() numbers were almost always within 1% of each other (len() was consistently a tad faster). I also tried it with longer strings, but that made no discernable difference.

  9. Matt Avatar
    Matt

    IMHO, Len(str) is just easier to read. Not sure how many folks remember, but when MX first came out, there were many discussions about the source (java) code that was compiled from the .cfm templates. If you take a look at the java code, you’ll notice that the Java created using the two different functions is almost identical. That’s probably why, in most cases, the execution times are about identical (depending on the system executing the call).

Leave a Reply