Adobe Max 2007 – Optimizing AS3

Optimizing AS3 Performance

Flash Player AVM2 Architecture JIT (2x compiled bytecode and JIT

When should I optimize?

  • Premature optimization can be a bad thing
  • Potentially added code complexity
  • Potentially increasing th number of lines of code just for perforemance
  • potentially degraded readability
  • Extra billable time spent optimizing when it coulf be spent on functionality

How should I optimize?

  • Use good coding practices
  • know your perfomrance goals
  • Profile your application before opimizing to determine the bottlenecks
  • Don’t over optimize
  • Optimize in incriments
  • Use Version Control

Tools

  • flash.utils.getTimer():int
  • trace
  • Flashdebug player
  • Flex Builder 3 Performance Profiler
  • REDbug
  • Firebug
  • FlashTracer

General Optimization Tips

  • Code Efficiently
  • Graphics can cause extreme performance hit if used incorrectly…so
    • Minimize the number of on-screen objects
    • Minimize
  • Optimize your memory footprint
    • Only load in instances of what you need
    • Make use of deffe
    • Remove references or make them weak references

    Remove debug Code

Mmxmlc -optimize=true -output HelloWorld

Optimizing Object Interaction

NON-OPTIMIZED

public function onInit()
}
var myFirstVar
}

OPTIMIZED

public function onInit()
}
var myFirstVar:int;
}

Unnecessary Object Definitions

for(var:int=0;i<1000000;i++
{

}

Use weak References and Array notation when accessing class members

Keep Arrays densely packed

Create a constant and use it within the app – Object instantiation is a huge process hog. Use constants whenever possible.

Take all calculations out of  loops.

Don’t use

while(i–)
{
var myReturn = a-b*c/d+e*g +i;
}

Use

var bob = a-b*c/d+e*g;

while(i–)
{
var myReturn = bob +i;
}

Try Catch is much slower than conditionals

Bitwise operators are much faster than with math.

Create a local variable referencing getters and setters and then call the members.

don’t use

Application.aplication.whatever

instead use

var myAppFriend:Application = Application.apllication.whatever

Build a class that I just need to throw in blocks of code and use getTimer() to preview time to execute.