ActionScript 3 optimization tips
Tweet
This article will show you a collection of optimization tips for ActionScrip 3 that you can use to speed up your application’s performance.
Let’s begin with the first rule in AS 3 optimization tehniques:
1. Use integers for iterations. Int type performs faster than Number when looping through the items of an array. This is because array indexing is faster with ints.
for(var i:int = 0; i < 1000000; i++);
2. Use shorthand operators whenever possible
Incrementing is faster using the "++" operator than using "i = i + 1". It is the same situation with "a += b" which is faster than "a = a + b";
3. Use multiplication instead of division when dividing by 2.
In this case it is better to use multiplication with .5 than division by 2.
Slow:
var a:Number = b / 2;
Fast:
var a:Number = b*.5;
4. Use bitwise shift operators for even faster performance.
Slow:
var a:int = 10;
a = a * 2; //returns 20
a = a * 4; //returns 40
a = a / 2; //returns 5;
Fast:
var a:int = 10;
a = a << 1; //returns 20
a = a << 2; //returns 40
a = a >> 1; //returns 5
5. Use single line variable declaration
Slow:
var a:int = 1;
var b:int = 2;
var c:int = 3;
Fast:
First use the "var" keyword and then declare the variables on a single line.
var a:int = 1, b:int = 2, c:int = 3;
6. Use "null" intead of "try .. catch" if possible:
var myArray:Array;
if(myArray != null)
myArray.pop();
else
trace("its null");
is way faster than:
try{
myArray.pop();
}catch(e:Error){
trace(e.message);
}
7.Use int(a) instead of Math.floor(a). Math.floor(a) is 10 times slower than int(a).
Slow:
for(var i:int=0; i<1000000; i++){
var a:int = Math.floor(1.5);
}
Fast:
for(var i:int=0; i<1000000; i++){
var a:int = int(1.5);
}
This is how you can test your code performance:
for(var i:int=0; i<1000000; i++){
var a:int = 4 << 1;
var b:int = 4 << 2;
var c:int = 4 >> 1;
}
trace(getTimer());




Nice! I only did not quite get the bitwise part of the optimization techniques. Using bitwise operators is hard for a newbie. I believe that this will certainly be more appealing if you include it in a real-world example.
this is great (the bitwise operations overall)
the only problem is that int() doesn’t work the same way as Math.floor(), for negative numbers it returns a wrong value.
int(-3.1) = -3; <- wrong
Math.floor(-3.1) = -4; <- correct
… you have been warned
Very good point, cyberpunk!
Well, it’s not a wrong value. Simply the “int()” function is different than “Math.floor()” and returns different value. So, we can say this technique works only for positive values. I have to make an update. Thanks
Sorry to tell you but number 5 is not correct.
I set up 2,600 int’s, boolean’s, strings, movieclip’s and my very own custom class’s.
Ran them from within the timeline in flash IDE and a class file.
ran each 15 times, and there was no difference in speed what so ever.
even using your very own “testing” function and 2 others I found on the net.
and just to be sure, I ran them all on a Quad-core, Duel-core, Single-core, Very Old computer and even a low powered netbook.
all returned the same results, and in about 5% of cases, even found that all on there own line was 1ms faster.
So I think its up to individuals taste, I personally prefer them all on there own line as its easier to read.