First, better to say that optimize performance is something to do at the end.
Some of the code can be much faster but less readable, so better leave this for the end of the project.
array.
When you create an array
[] is 3 times faster than new Array() ( same for Object , {} 3 time faster than new Object() )
//wrong
var arr : Array = new Array( 1 , 2 , 3 , 4 ) ;
for( var i : int = 0 ; i < arr.length ; ++i )
{
//
}
//good
var arr : Array = new Array( 1 , 2 , 3 , 4 ) ;
var l : int = arr.length ;
var i : int ;
for( i = 0 ; i < l ; i++ )
{
//
}
So, calculate the length of array before the loop, is a massive speed up when loopin an array.
And remember
Arrays do take a considerable amount of time to create.
Retrieving a value is almost twice as fast as setting a value.
Array VS Vector
Array and Vector are similar in performance. You will see a big difference in performance when you do this
var c : Boolean = myArray[ j ].booleanProp
//this is slower than this :
var obj : Object = myArray[ i ] ;
var c : Boolean = obj.booleanProp
// that is similar if you use Vector as this
var arrVector : Vector.
Avoid push(), pop(), shift() or unshift();
if else VS switch.
If else or switch doesnt have a relevant difference.
++
++i or i++ doesnt have any relevant difference, but better than i = i + 1
int and uint.
int is significantly faster than uint, and miles faster than Number, used as iterator in a loop
so never for( var i : uint = 0 ...
moltiplication and division by power of 2.
if you need an integer, divide by 2 in this was. this run almost double faster.
Here the rest of operation
var nr : int = 10 >> 1 // var nr : int = 10 / 2
var nr : int = 10 >> 2 // var nr : int = 10 / 4
var nr : int = 10 << 1 // var nr : int = 10*2
var nr : int = 10 << 2 // var nr : int = 10*4
var nr : int = 10 << 4 // var nr : int = 10*16
getter and setter.
Are your enemy! in any loop try to have direct access to variable, and avoid any call to other methods or getter and setter. ( now you can understand why we should optimize later, closer to the end ).
Direct access of class members is three times as fast as accessing via a function.
Math.floor VS int
Math.floor( number ) is 10 time slower than int( number )
Math.abs VS ifelse
Math.abs( number ) is A LOT slower than
number = number < 0 ? -number : number
//or this even faster
if( number < 0 ) number = -number ;
Math.ceil VS ifelse
Math.ceil( number ) is damn slower than
number = number == int(number) ? number : int(number)+1
Math.sin or Math.cos
Use a lookiup table if you are using only integer angle. you will save time in calculation of sin, and conversion between radians and degree
What you can do
Give your variables decent names, you are not increasing performance by using shorter names or abbreviations.




