String concatenation is extremely slow when input is large

Things like the code below are very slow:

var str:String = ""
for (var i:Number = 0 ; i<1000000000000000000 ; ++i) {
    str += "someLongLongLongLongLongLongLongLongLongString";
}

There is in Java StringBuilder, but there seems to be no equivalent for AS. So how do you guys handle concatenating large strings?


Update:

Thanks for the answer!

I just coded my own testing program . Usage is +=already the fastest ... What's slower than putting it on TextArea ...

I voted for most of you, because the tips make sense :) although my test result shows that my question is somewhat problematic, as I ask for something better than what is already better: P

+3
source share
5
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
performancetests.Strings (1 iterations)                                 
Player version: MAC 10,0,32,18 (debug)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
stringsWithConcatMethod                                   17555 17555.00
stringsWithPlusConcat                                      4972  4972.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

, 10000000:

var str:String = ""
for (var i:Number = 0 ; i<10000000 ; ++i) {
    str += "someLongLongLongLongLongLongLongLongLongString";
}

var str:String = ""
for (var i:Number = 0 ; i<10000000 ; ++i) {
    str = str.concat("someLongLongLongLongLongLongLongLongLongString");
}

Grant Skinner AS3 Performance Test Harness. String:: concat , , as3, , . (1 1 10000000). . , concat .

+2

, , StringBuilder. : 46 ?

- (.. ) , , , ( ).

, , . . , : -)

+6

, ActionScript, ECMAScript , ( JavaScript ):

var sb = [];
for (var i = 0; i < 10000000000; i++) {
    sb.push('longlonglong');
    // In this particular case you can avoid a method call by doing:
    //sb[i] = 'longlonglong';
}
var str = sb.join('');
+5

:

  • , . . , , , .
  • , O (1), , , . , .

, ( , , ActionScript).

+2

...: D... AVM2, ... unicode, 2 char... ascii i-will-make-your-heap-explode... sh * tload , flash.utils::ByteArray... , , ... , ...: P

+2

Source: https://habr.com/ru/post/1719590/


All Articles