AS3 Optimisation Tip I: Use public properties

// 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?

Tags: , , ,

One Response to “AS3 Optimisation Tip I: Use public properties”

  1. spender Says:

    in some languages i've used, this would be considered bad if the code exists in a linked library, because it allows for no refactoring flexibility… code that were dependent on the library would require recompiling should a public field be changed to a setter/getter in the library. hence there is a preference for public setters/getters wrapping private variables to allow insertion of logic/validation should the need arise without the penalty of recompiling all dependent projects. as runtime importing of code in AS3 is rare, this would rarely be an issue here.

Leave a Reply