No, the "new" code is less efficient when used in a heavy loop.
You can emulate StringBuffer with an array.
<cfset var buffer = ArrayNew(1)>
<cfset ArrayAppend(buffer, variables.myVar1)>
<cfset ArrayAppend(buffer, variables.myVar2)>
<!--- more of these --->
<cfset result = ArrayToList(buffer, "")>
However: If you are not concatenating hundreds or thousands of strings in a row, there is little benefit in using StringBuffer. You can go with &
in most cases.
I have done some testing (on a ColdFusion 7 server w/ Java 1.4, more recent editions might score differently).
All times are milli-seconds The results are averaged over 10 repeats (except the one in square brackets as this took virtually forever).
The following is the measurement of iteratively concatenating a single letter N times.
iterations concat array cfsave string
content buffer
100 0 0 0 2
1000 4 4 2 22
10000 168 29 33 219
100000 30,781 168 293 1,575
The following is the result of iteratively concatenating a string of length 10 N times:
iterations concat array cfsave string
content buffer
100 0 0 0 4
1000 25 0 2 24
10000 1,242 33 31 232
100000 [410,979] 180 373 2,082
Note how basic string concatenation gets rapidly more inefficient the more iterations you do and the longer the string gets. The other methods show more linar behavior.
Further notice that the StringBuffer's performance is also tied to its initial size. If you dimension it too small (i.e. by calling init("")
instead of .init(10000)
, for example) then it is forced to re-allocate more space when it runs out, which also takes time. For the sake of test, I initialized it with exactly the amount of space it would need.