Monday, February 28, 2011

Using the Java StringBuilder Class to Increase ColdFusion String Performance

One of the commonly known best practices in Java programming is to use the java.lang.StringBuilder class (read more here) when you are doing a lot of string concatenation operations.  The reason is because Java String (and by association ColdFusion strings) objects are immutable, which means that once a String is created its internal state is unchangeable.  So this means that every time you are changing a string, Java is actually creating a new string object behind the scenes and re-assigning it to your variable.  This got me thinking about the performance implications of using the StringBuilder class in my ColdFusion applications.

For this test I created two templates, one using the & operator to construct a large string and the other using the StringBuilder class to do the same operation.  Below is the code from both of the files (with most of the test text removed however I used the exact same text string for both files):

Using the & operator:

<cfscript>
 writedump(NOW());
 
 stop = false;
 
 myAppendedText = "";
  
 myText = "Lorem ipsum dolor sit amet, ...";
 
 myWordArray = listToArray(myText," ");
 
 for(i =1; i <= 2500; i++){
  
  myAppendedText &= myText;
 }
 
 writedump(NOW());
</cfscript>

Using the StringBuilder class:

<cfscript>
 writedump(NOW());
 
 stringBuilder = createObject("Java","java.lang.StringBuilder");
 
 stop = false;
 
 myAppendedText = "";
  
 myText = "Lorem ipsum dolor sit amet, ...";
 
 myWordArray = listToArray(myText," ");
 
 for(i =1; i <= 2500; i++){
  
  stringBuilder.append(myText);
 }
 
 writedump(NOW());
</cfscript>

The results were quite convincing.   Using the & operator the script ran in 23 seconds, the template using the StringBuilder object ran the code in under 1 second.  So the next thing I wanted to do was to see how well the ScriptBuilder method would scale so I increased the number of times the loop ran that added text to 5500 and the ScriptBuilder method still ran in under a second!

The moral of the story is that if you have to do lots of string concatenation the clear choice is to use the StringBuilder class.  I do stress that the effectiveness of this method only works under situations where you are doing A LOT of string concatenation.  The performance difference between the two methods was very minimal until I started to create some pretty big strings, but once I hit that threshold the StringBuilder significantly outperformed using the & operator.
Fork me on GitHub