Search code examples
actionscript-3incrementgetter-setterdecrement

Actionscript 3.0 getter setter increment


private var _variable:int;

public function set variable(val:int):void{

        _variable = val;

}
public function get variable():int{

     return _variable

}

Now if I have to increment the variable... which one is more optimized way of doing ?

__instance.variable++;

or

__instance.variable = __instance.variable + 1;

The reason for asking this question is, I have read a++ is faster than a = a+1;. Would the same principle apply even when using getters and setters ?


Solution

  • No normally they will be translated the same way because there is no special opcode within the VM to do this operation, the VM will have to do these operations :

    • read the variable value into a register
    • increment the register
    • put back the value

    now it's shorter and less error prone to write __instance.variable++ than the second way.

    In contrary when you increment a local variable doing var++ it exists a special operation (inclocal or inclocal_i (i stand for integer) ) that will directly increment the value of the register so it can be slightly faster.

    Here a list for example of the AVM2 opcode : http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html