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
Methods used are:
- str += concateString;
- str = str.concat(concateString);
- array.push(concateString); … str = array.join(“”);
- vector.push(concateString); … str = vector.join(“”);
- byteArray.writeUTFBytes(concateString); … str = byteArray.readUTFBytes(byteArray.length);
- 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:
- str += concateString;
- str = str.concat(concateString);
- 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?