AS3 Optimisation Tip II: Avoid String concatenation

// this:
var id:int = 0;
var name:String = id.toString();
 
// as opposed to this:
var id:int = 0;
var name:String = "name_" + id.toString();
 
// and ESPECIALLY this:
var id:int = 0;
var name:String = "obj_" + "name_" + id.toString();

Conclusion: each additional concatenation adds roughly an additional 20% to processing time

Twenty percent is a lot if you’ve gotta run through a long loop and assign properties to a whole bunch of properties. Anywhere where you can avoid concatenation you should. Storing values indexically with integers seems by far the best way to go with this, whether you are storing your data in Arrays, Dictionaries, Objects or your own bespoke class. Concatenation is going to drag you back wherever you use it.

Tags: , , ,

One Response to “AS3 Optimisation Tip II: Avoid String concatenation”

  1. spender Says:

    this comes up frequently especially with strings used to hold a log of some sort…

    function addMessage(msg:String)
    {
    log+=msg+”n”; //really very bad… with enough log output, this will sink the ship
    }

Leave a Reply