Just recently I have needed an in depth understanding of AS3 efficiency practises, so I figured this is easiest done by getting involved in a series of relevant tests. Though there are similar blog posts regarding this most have been focused on either loops or Number types and this article won’t repeat these tests as I too discovered similar suggestions.
Here are my tests :
Array & Object constructing
new Array() and new Object() are about 3 times slower than [] and {}. Though further to this [] is much slower than [].concat() so if you have a constant reference to an empty Array use [].concat().
Index Number type for Arrays
In AS2 list[int(0)] was faster than list[0] this is no longer the true. list[0] is now the faster option. list[int(0)] is now faster than list[0]!
Create Array vs. Updating Array
If you can avoid it don’t constantly create Arrays/Objects this is especially the case in long loops rather create them once then update.
Nulling Array vs. Splicing Array
When working with large Arrays splicing is obviously an expensive operation, you can avoid this by nulling the index and skipping it in a null scenario. If you need to splice in order to keep the Array length low. Then store these nulled indexes in another trash Array once the garbage count has reached a limit you’ve defined loop through the numerically sorted trash indexes deleting splices in bulk. This concept is demonstrated in Tweensy.
Nulling Object vs. Delete Object
Deleting an item in an Object is always more extensive than nulling it. If you’re using a Dictionary try to use weak references in that case once the key is dereferenced the Flash garbage collector will eventually remove the item from the Dictionary.
Nesting Loops
Nesting loops is always slower try to minimize your nest depth to no more than 2. As well always use for loops if you can they are the fastest.
Inline code vs. function references
When executing a function it’s more expensive if you call other functions within it, so if the function is an enterframe function or long loop try to minimize referencing alot of functions for best performance.
Arguments vs. variable referencing
Arguments in functions are slower than a reference to an objects variables so try to minimize the number of arguments you use.
Function apply scoping do it or not?
Scoping function.apply is a little bit slower than not so if you don’t have to then don’t.
Array push vs. Array index
Don’t use the push function on an Array instead use the Array length property and set on the Array like so list[list.length] = data;
Or if that’s not enough for the best performance (about 600% faster) than using push don’t use the Array length property at all. To do this keep the Array length as an external variable and increment/decrement it as you add and remove items from the Array.
Array emptying – length 0 vs. A new Array
If you need to empty an Array there is a handy option to do this via setting the Arrays property length to 0. You would think this would be the better option as you are avoiding creating a new Array and ultimately saving memory. Though it’s interesting to know as a trade off using length to clear an Array is not faster in performance (in most cases). Using length is more efficient when you might need to clear more than 510 Arrays in a single execution, otherwise creating a new Array via [] is the faster option again.
Var declarations on multiple lines vs. Var declarations on a single line
If you need to declare a few variables it’s slightly more efficient to do so on a single line i.e.var a:int=0, b:int=0, c:int=0;
vs.var a:int=0;
var b:int=0;
var c:int=0;
Using Xor to swap variables
Using Xor is very handy if you want to swap variables but don’t want to create a third variable i.e.
a = a^b;
b = a^b;
a = a^b;
Like most people I thought this would be more efficient but it’s not compared to creating a third variable and then doing the swap. In fact it’s about 300% more efficient to do the swap this way. Though honestly I still have a soft spot for the Xor method.
var oldB:int = b;
b = a;
a = oldB;
Multiplication vs. Division
When working with Math in Actionscript multiplications seem to always run faster than divisions so instead of 5000/1000
use 5000*0.001
it’s about 130% faster.
Type casting comparison
When type casting the keyword as
is 250% more efficient than casting by Type(item);
Though surprisingly not using either is about 1400% more efficient.
Long vs Short variable names
Though this one is debatable because it results in illegible code. Shortened variable names do give a millisecond or so improvement if iterating many 1,000’s of times in a single loop. Though I think this is a last resort in code optimization as the differences are really marginal.
More to read: AS3 SWF Profiler