String Concatenation

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


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.

Share on Google+Share on FacebookShare on LinkedInPin on PinterestTweet about this on Twitter

4 thoughts on “String Concatenation”

  1. Thanks for proving this out, it answered my question and saved me time. Flex continues to impress me …

  2. 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());
    text = text.concat(temp);

    return text;


  3. 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.


Leave a Reply

Your email address will not be published. Required fields are marked *