Flash String Concatenation – Keep it Simple
I’ve seen a lot of Flash libraries attempt to increase their speed in string intensive tasks by creating a custom StringBuffer class.
In many other languages, a StringBuffer is much faster than standard string concatenation. I set out to see just which method was fastest, for both the tasks of combining a handful of strings (<50) and combining a large amount of strings (>10000).
Here is the test:
// test string buffer approaches
var start_time:uint, i:int, j:int;
var bufferOut:String;
// normal concatenation approach, 50 segments
start_time = getTimer();
for (i=0; i<10000; i++) {
var buffer_1:String = '';
for (j=0; j<50; j++) {
buffer_1 += '1234567890123456789012345678901234567890';
}
bufferOut = buffer_1;
}
trace('CONCATENATION: 50 strings per buffer, 10000x = '
+ (getTimer() - start_time));
// normal concatenation approach, 10000 segments
start_time = getTimer();
for (i=0; i<50; i++) {
var buffer_2:String = '';
for (j=0; j<10000; j++) {
buffer_2 += '1234567890123456789012345678901234567890';
}
bufferOut = buffer_2;
}
trace('CONCATENATION: 10000 strings per buffer, 50x = '
+ (getTimer() - start_time));
// vector.string approach, 50 segments
start_time = getTimer();
for (i=0; i<10000; i++) {
var buffer_3:Vector.<String> = new Vector.<String>();
for (j=0; j<50; j++) {
buffer_3.push('1234567890123456789012345678901234567890');
}
bufferOut = buffer_3.join('');
}
trace('VECTOR: 50 strings per buffer, 10000x = '
+ (getTimer() - start_time));
// vector.string approach, 10000 segments
start_time = getTimer();
for (i=0; i<50; i++) {
var buffer_4:Vector.<String> = new Vector.<String>();
for (j=0; j<10000; j++) {
buffer_4.push('1234567890123456789012345678901234567890');
}
bufferOut = buffer_4.join('');
}
trace('VECTOR: 10000 strings per buffer, 50x = '
+ (getTimer() - start_time));
// array approach, 50 segments
start_time = getTimer();
for (i=0; i<10000; i++) {
var buffer_3a:Array = [];
for (j=0; j<50; j++) {
buffer_3a.push('1234567890123456789012345678901234567890');
}
bufferOut = buffer_3a.join('');
}
trace('ARRAY: 50 strings per buffer, 10000x = '
+ (getTimer() - start_time));
// array approach, 10000 segments
start_time = getTimer();
for (i=0; i<50; i++) {
var buffer_4a:Array = [];
for (j=0; j<10000; j++) {
buffer_4a.push('1234567890123456789012345678901234567890');
}
bufferOut = buffer_4a.join('');
}
trace('ARRAY: 10000 strings per buffer, 50x = '
+ (getTimer() - start_time));
// bytearray approach, 50 segments
start_time = getTimer();
for (i=0; i<10000; i++) {
var buffer_5:ByteArray = new ByteArray();
for (j=0; j<50; j++) {
buffer_5.writeUTFBytes('1234567890123456789012345678901234567890');
}
buffer_5.position = 0;
bufferOut = buffer_5.readUTFBytes(buffer_5.length);
}
trace('BYTES: 50 strings per buffer, 10000x = '
+ (getTimer() - start_time));
// bytearray approach, 10000 segments
start_time = getTimer();
for (i=0; i<50; i++) {
var buffer_6:ByteArray = new ByteArray();
for (j=0; j<10000; j++) {
buffer_6.writeUTFBytes('1234567890123456789012345678901234567890');
}
buffer_6.position = 0;
bufferOut = buffer_6.readUTFBytes(buffer_6.length);
}
trace('BYTES: 10000 strings per buffer, 50x = '
+ (getTimer() - start_time));
The results of this test:
CONCATENATION: 50 strings per buffer, 10000x = 142 CONCATENATION: 10000 strings per buffer, 50x = 106 VECTOR: 50 strings per buffer, 10000x = 335 VECTOR: 10000 strings per buffer, 50x = 195 ARRAY: 50 strings per buffer, 10000x = 340 ARRAY: 10000 strings per buffer, 50x = 234 BYTES: 50 strings per buffer, 10000x = 711 BYTES: 10000 strings per buffer, 50x = 756
Simple string concatenation is the fastest method, which is nice since it's also the most simple.
Vector and Arrays take twice as long and writing to a ByteArray takes five times as long. Keep it simple!

REally powerful script in my opinion, also test results seem promising, will work on it myself. Thanks for the post!