Archive for October, 2007

We need help testing web services!

We are asking for all Flex 3 customers to help test out the latest approved nightly build on Adobe Labs with any WebServices that they consume.

See Matt’s blog entry for details:

Please help us out!!

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++)
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.