I am working on exporting DataGrid content to CSV (I will present result soon) and one question came to my mind:
Is ActionScript 3 String concatenation efficient?
After some googling I have found Peter Farland blog post which suggested using String.fromCharCode() method instead of string concatenation. Although my case was a little bit different because I was to concatenate whole item labels (not single characters) I decided to create simple StringBuffer based on fromCharCode().
Here is the code of my StringBuffer class:
public class StringBuffer { public var buffer:Array = new Array(); public function add(str:String):void { for (var i:Number = 0; i < str.length; i++) { buffer.push(str.charCodeAt(i)); } } public function toString():String { return String.fromCharCode.apply(this, buffer); } }
To check the performance of my StringBuffer I have created simple test (available below).
In this test I first generate array of strings (simulating labels from DataGrid) and then concatenate labels using standard “+” operator and my StringBuffer. The result surprised me a lot: standard String concatenation is about 7 times faster than my StringBuffer. After some playing with commenting out different lines of code in my StringBuffer I have found out that standard String concatenation is even faster than iterating (without any array modification) over all labels characters! It seems that Flash Player has some well optimized low level code for handling string concatenation and there is no reason to try to do it beater.
Conclusion:
If you have set of String values and want to concatenate them don’t hesitate to use standard concatenation (string1 + string2) !
This is the test used to compare String concatenation performance (don't expect nice code - it is just a test!) View source is enabled, you can download zipped sources from here.
Thanks for proving this out, it answered my question and saved me time. Flex continues to impress me …
Thanks posting this … we were just about to create this test ourselves 😉
I’m currently learning flex and I ran into this problem. Unfortunately Flex array handling seems to be pretty slow. Here is the solution I adopted;
private function readASCII(length:int):String
{
var text:String = “”;
while (length > 0) {
var temp:String;
if (length >= 8)
{
temp = String.fromCharCode(inputBuffer.get(), inputBuffer.get(), inputBuffer.get(), inputBuffer.get(),
inputBuffer.get(), inputBuffer.get(), inputBuffer.get(), inputBuffer.get());
length = length – 8;
}
else if(length >= 4)
{
temp = String.fromCharCode(inputBuffer.get(), inputBuffer.get(), inputBuffer.get(), inputBuffer.get());
length = length – 4;
}
else if(length >= 2)
{
temp = String.fromCharCode(inputBuffer.get(), inputBuffer.get());
length = length – 2;
} else {
temp = String.fromCharCode(inputBuffer.get());
length–;
}
text = text.concat(temp);
}
return text;
}
Thanks for doing this. Extensive string concatenation with plus operator is definitely a no-no in the C# world, much better to use StringBuilder class.
So it is really good to know that one operator does the trick in Actionscript.
regards…