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.