Avoiding String Concatenation

When writing a String based encoder in ActionScript you quickly find yourself needing to build up the encoded String a character or so at a time. Without a StringBuffer type in ActionScript you may be tempted to use String concatenation as a simple method to build up a String. The problem with this approach is a number of temporary Strings are created as a result and will hang around until garbage collection catches up.

for (var i:uint =0; i < source.length(); i++)
{
result += encodeSomething(source.getCharAt(i));
}

To avoid large memory spikes, a better approach is to build up an Array of character code points as integers and then using them to construct a String in one step using the String.fromCharCode() function:

var buffer:Array = [];
for (var i:uint =0; i < source.length(); i++)
{
buffer.push(encodeSomethingAsInt(source.getCharAt(i)));
}
result = String.fromCharCode.apply(null, buffer);

Since fromCharCode takes a variable number of arguments, we use Function.apply to pass the Array of integers in a single call.

Note that the arguments of Function.apply end up on the stack, so to avoid a stack overflow you should limit the size of the array to around 64,000 entries. You can always concatenate successive buffers into a final String.