Please note, new blog at

Using Arrays for String Concatenation

In response to my previous post about using Java for String concatenation instead of ColdFusion’s concatenation operator, Greg has given me an interesting tip. It turns out that ArrayAppend() is a LOT quicker than regular concatenation, and the StringBuffer class! If you’re generating a CSV string, all you have to do is put each line into an array element using the ArrayAppend() method. Once all lines have been appended, convert the array to a list using the ArrayToList() method, using #chr(13)##chr(10)# as the delimiter. I decided to run the same sorts of tests that I have before. This time, I compared concatenating a string with regular string concatenation, the ArrayAppend method and the Java StringBuffer class. Using ArrayAppend was certainly the quicker of the three – by a long way. So, if you’re building a small-medium length string, then ArrayAppend is the way to go. However, a word of caution. If you are creating a large CSV file, then use the Java methods I have discussed before.


Code Examples

Regular string concatenation <cftimer type="inline" label="Regular concentation"> <cfscript> iNrTimesToLoop =; sStringToConcat = "The quick brown fox jumped over the fence: "; sString = ""; for (x=1; x LTE iNrTimesToLoop; x=x+1) { sString = sString & sStringToConcat & x; } </cfscript> </cftimer> ArrayAppend <cftimer type="inline" label="ArrayAppend"> <cfscript> iNrTimesToLoop =; sStringToConcat = "The quick brown fox jumped over the fence: "; aString = ArrayNew(1); for (x=1; x LTE iNrTimesToLoop; x=x+1) { temp = ArrayAppend(aString, sStringToConcat & x); } // Convert back into a string sString = ArrayToList(aString, "#chr(13)##chr(10)#"); </cfscript> </cftimer>

By Anonymous mike lyda, at 10/03/2005 03:24:00 am  

I put together a similar test for Flash.. diferent results entirely of course..
(test #15)

By Anonymous Rahul Narula, at 10/06/2005 03:57:00 am  

<cftimer type="inline" label="ListAppend">
iNrTimesToLoop =;
sStringToConcat = "The quick brown fox jumped over the fence: ";
aString = "";

for (x=1; x LTE iNrTimesToLoop; x=x+1)
temp = ListAppend(aString, sStringToConcat & x);


We achieve better times as we avoided using 2 function calls
- ArrayNew(1)
- ArraytoList()

By Anonymous Rahul Narula, at 10/06/2005 04:03:00 am  

try > 20000 to see some appreciable diff between the 2 methods

By Blogger J&D, at 9/18/2009 10:42:00 pm  



By Blogger longge, at 6/26/2010 01:15:00 pm  

While traveling anywhere and the Louis vuitton bags
is the one which makes you beautiful wherever you go. This is not alone due to the architecture of the atypical louis vuitton handbags
. lv
accept got their own accent in our lives, for arcade or anywhere abroad after accustomed a louis vuitton
with us.

By Blogger dingdang, at 6/28/2010 04:58:00 pm  

One of the ultimate appearance accessories in the apple is the hermes handbag
. Why is the Kelly bag
such a account of elitism?One of the affidavit is due to the superior of the Kelly bags
Currently supports and how to use open-source tools to construct a GOMER PYLE
that can play on almost every GOMER PYLE DVD
is comprised of one or more video title sets.

» Post a Comment


» Create a Link