Apr
26

Actionscript 3.0 Optimization Techniques

So I ran across this pdf by Joa Ebert recently about Actionscript 3 optimization techniques. It was a good read, nothing that hasn’t already been stated before, but I figured I’d touch up on some of the major points here in this blog. This post is especially for AS2 developers that are still in the old AS2 mode of typing.

Native Types

One of the basic things to remember is to use integers instead of numbers when iterating in a loop. Heres an example…

// Slow Version
for (var i: Number = 0; i <n; i++)
   void;       
 
// Fast version:
for (var i: int = 0; i < n; i++)
   void;

Typing

Strict type your objects. In AS2 creating objects dynamically using the Object class was a very nasty habit. Instead create the class specifically.

// Slow version:
for (;i<n;i++)
{
   var v: Object = new Object;
   v.x = 1;
   v.y = 2;
   v.z = 3;
}
// Fast version:
class Vector3D
{
   public var x: Number;
   public var y: Number;
   public var z: Number;
}
for (;i<n;i++)
{
   var v: Vector3D = new Vector3D;
   v.x = 1;
   v.y = 2;
   v.z = 3;
}

Casting

Array access can be speeded up by telling the Flash player what is inside the array. Since arrays can contain any object the player has to gure out what type it is. This step can be eliminated by casting the object.

// Slow version:
for (;i<n;i++)
   array[i].x = 2;
 
// Fast version:
for (;i<n;i++)
   Vector3D(array[i]).x = 2;

Promoting Issues

If you do something like i * 2 the expression will be promoted as a Number. Array access has been optimized for integer numbers. So whenever you do some calculations make sure that you keep the int type by casting again as an integer.

// Slow version:
for (;i<n2;i++)
   Vector3D(array[i*2]).x = 2;
 
// Fast version:
for (;i<n2;i++)
   Vector3D(array[int(i*2)]).x = 2;

Instance re-using

Using a lot of uneeded istances slows the Flash player down in two ways. The first is that a lot of garbage is created which means the garbage collection has to do extra work. The second argument against a lot of unneeded instances is simply that you have to create them which takes an extra amount of time.

// Slow version:
for (;i<n;i++)
   p = new Point(i, i);
 
// Fast version:
for (;i<n;i++)
{
   p.x = i;
   p.y = i;
}

try..catch and null

What I always tell my team is to never use try catch unless you’re trying to access something external to the flash player like files, web services, etc. and only if you’re going to notify the user of what went wrong in the exception. Other than that use nulls like so…

// Slow version:
// Slow version:
var o: Sprite;
for (;i<n;i++)
{
   try
   {
      o.blendMode = BlendMode.ADD;
   }
   catch ( error: Error ) {}
}
 
// Fast version:
var o: Sprite;
for (;i<n;i++)
   if ( o != null )
      o.blendMode = BlendMode.ADD;

Alot of these optimization tricks not only optimize your code in runtime. But alot of these tricks are good for code readability. I can’t emphasize enough how good it is to get in the habit of creating classes instead of dynamically creating objects, clear and concise commenting, and abstracting your code to manageable classes. In the end, it will make you a better programmer.

Tagged with:


 

 

 

comments

One Response to “Actionscript 3.0 Optimization Techniques”

  1. Investigación sobre optimización en aplicaciones desarrolladas con Flex (Parte 2 – planteando soluciones) « 3wstudio Says:

    [...] http://blog.jodybrewster.net/2008/04/26/actionscript-30-optimization-techniques/ [...]

leave a reply