Posts Tagged ‘Optimisation’

AS3 Optimisation Tip II: Avoid String concatenation

Tuesday, August 26th, 2008
// 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.

AS3 Optimisation Tip I: Use public properties

Thursday, August 7th, 2008

// this:
public var id:int;
 
// as opposed to this:
private var _id:int;
public function get id ():int
{
    return _id;
}
public function set id (id:int):void
{
    _id = id;
}

Conclusion: Public properties are roughly 20% faster than private vars with getters & setters

If you don’t need to run any code on a class property why bother writing a getter and a setter for it? It’ll take longer to type (FDT users aside), make your code fatty and run slower. Tell me the advantage in all that. Instead, why not just use a public variable with the caveat that if you need to turn it into a private variable with a getter and a setter later you can?