AS3/JS string concatenation methods performace test

October 9th, 2009  |  Published in Uncategorized  |  4 Comments

When writing BrainFlash, I was thinking if the string concatenations for program output will slow down the whole interpreter. That is because I was once hit that a year ago when dealing with XML.

I asked for better handling methods over StackOverflow. It is interesting that some of the answers shown that using “+=” is already the fastest. But I still want to know more about that, so I wrote my own simple testing program, which is below:

ActionScript version

http://get.adobe.com/flashplayer

source code

Methods used are:

  1. str += concateString;
  2. str = str.concat(concateString);
  3. array.push(concateString); … str = array.join(“”);
  4. vector.push(concateString); … str = vector.join(“”);
  5. byteArray.writeUTFBytes(concateString); … str = byteArray.readUTFBytes(byteArray.length);
  6. byteArray.writeMultiByte(concateString,”us-ascii”); … str = byteArray.readMultiByte(str.length,”us-ascii”);

The result shows that fastest method is using “+=”, but using Array/Vector is still very close to it. Using ByteArray is slow and with ASCII instead of UTF-8 is even slower…

And all the methods are performed reasonably fast, what is slow is when showing the resulting string on TextArea… It takes a few seconds! But when it is drawn, run it again and it will become normal speed, maybe there is some caching?

JavaScript version

I coded a JavaScript port too. See it here.

Methods used are:

  1. str += concateString;
  2. str = str.concat(concateString);
  3. array.push(concateString); … str = array.join(“”);

Interesting enough, the result is very similar to AS3. It is the opposite of what we believe using the Array trick will let it performs faster. “+=” is the fastest in most cases, if not, that’s not much difference.

I’ve only tested in IE8(Win), Firefox 3 (Win/Mac), Safari 4 (Win/Mac), Chrome 3 (Win). The really really really interesting part is in Chrome 3, using the concat method of String is x7000 SLOWER than the other two!! What are your results?

Tags: , ,

Responses

  1. William Taysom says:

    October 12th, 2009 at 6:23 pm (#)

    mx.controls.TextArea is not particularly good. Though Adobe has the goal of making it better, spark.components.TextArea is currently much worse. For basic text output, flash.text.TextField works pretty well. If I were doing something fancy, I’d use flash.text.engine. It’s very speedy; however, you are required to layout lines of text all by yourself, and it doesn’t provide any support for editing. For fancy layout and editing, there’s flashx.textLayout. It’s not simple, and I doubt it’s as fast as more primitive routines: the more you do, the longer it takes.

  2. Andy says:

    October 15th, 2009 at 12:37 am (#)

    @William Thanks for the info! Sometimes Flash performance stuff really drive me crazy. For text stuff I do think old school HTML performs better, although it can’t do fancy stuff as Flash’s Text Layout Framework…

  3. Adam says:

    January 8th, 2010 at 12:20 pm (#)

    Thanks for the test! I’m used to having alternative methods to concatenate strings that are way more optimized than +=. Appreciate the post.

  4. Paul Morris says:

    April 2nd, 2010 at 3:43 am (#)

    Great post! I was looking around for some reliable information on this since I am familiar with StringBuilder in C# but wanted to know the best way to handle this in AS3.

Leave a Response