Actionscript optimizations tutorial
[ January 27, 2004 ] by Marco Lapi, a.k.a Lapo
In this article we discuss many different ways of optimizing your actionscript code. Also we've run a set of benchmarks to test typical game code bits and see how to squeeze the best performance out of the flash player.


Advanced optimizations:
Here follows a list of benchmarks that we have run to check typical code bits that are commonly used in games.
To do so we have also gathered info from some already existing and authoritative resources (listed at the end of this article) about actionscript optimizations and we've run these tests based on their advices, plus we experimented a bit with them and with other ideas as well.

The benchmarks were run on 2 different machines setup like this:

Desktop AthlonXP 2.6Ghz (VIA KT-400 chipset)
512mb RAM
Windows 2000pro
Notebook P4 2.0Ghz (Intel i845 chipset)
512mb RAM
Windows XP Home edition

Each benchmark was run three times and the final result is an average of the three values.

The results are expressed in ms. (milliseconds) and they represent the execution time of each test, so better performance is achieved with lower values.

A first set of tests was run using the flash player 6 then all the benchmark source files were compiled using Flash MX 2004 and exported for the player version 6.
It is very interesting to see how the new compiler optimizes the actionscript code and how different are certain results compared to the Flash MX compilation.

The detailed table with all the results of these benchmarks can be downloaded in PDF format.

AS Benchmarks
compiled with Flash MX - for flash player 6



download

AS Benchmarks
compiled with Flash MX 2004 - for flash player 6



download

The Benchmarks:
Here is a list of the tests run and some synthetic comments about their results.
Please refer to the PDF documents for detailed benchmarks results.

1) Attaching moviclips: for loop vs. while loop
A typical game loop where you attach movieclips to a target mc.
Using the while loop instead of for results in better performance, and if you're also
reading data from an array (this is shown in the next benchmark) then the for in loop should be the best choice.

So instead of using:

for (var i=0; i<1000; i++)
{
	// code here
}

You should use:

var i=-1
while (++i < 1000)
{
	// code here
}

2) Read data from Array (for, for in, while)
Here we compared the three loops in reading data from an array and the for in is definetely the best way to go.

arr = []
MAX = 5000

// Fill an array
for (i=0; i < MAX; i++)
{
	arr[i] = i
}


var item = null

// For Loop
for (var i=0; i < MAX; i++)
{
	item = arr[i]
}


// For In Loop
for (var i in arr)
{
	item = arr[i]
}


// While Loop
i = -1
while(++i < MAX)
{
	item = arr[i]
}

3) Write data to an array (for, while)
Writing data to an array also shows that the while loop performs better, even if this time the difference is less significant.

4) _global vars vs. Timeline vars
In this test we have checked if using _global variables can speed up variable access. We've recorded some extra performance with this approach, but not a significant speed increse.


5) Single vs Multiple var declaration.
Whenever possible try to assign values to your variables using multiple assignment.
This can speed things up pretty nicely.
If you have in a loop something like this:

a = 0
b = 0
c = 0
d = 100
e = 100

Use this instead:

a = b = c = 0
d = e = 100


6) Var name lookup
This test shows how important is saving name lookup time especially when insidea looping structure.
Instead of calling Math.random() at every cycle, define a reference to it and use it in the loop, this will save you time because the function name is resolved once, before entering the loop.

Example:

var num = null
t = getTimer()
for (var i=0; i < MAX; i++)
{
	num = Math.floor(MAX) - Math.ceil(MAX)
}
t1.text = "Always lookup: " + (getTimer() - t)


is not as good as:

t = getTimer()
var floor = Math.floor
var ceil  = Math.ceil
for (var i=0; i < MAX; i++)
{
	num = floor(MAX) - ceil(MAX)
}

7) Short vs. Long variable names
Variable names should always be kept as short as possible.
In this test you can see how long named variables can negatively affect speed.
We suggest to use very short names (1 or 2 chars) when optimizing performance critical part of your code.

8) Declaring var in loops vs. declaring before loops
Before running this benchmark we thought that declaring a local var before a loop
structure could save some xtra time, but tests proved the exact contrary.

// Inside
t = getTimer()
for (var i=0; i < MAX; i++)
{
	var test1 = i
}
t1.text = "Inside:" + (getTimer() - t)


// Outside
t = getTimer()
var test2
for (var i=0; i < MAX; i++)
{
	test2 = i
}

9) Using nested if
Whenever using complex conditional expressions you can optimize the code by breaking them into single nested conditions.
This way you don't have to evaluate all the expressions all the times.
The following code is what we used for the benchmark: even if the conditional expressions are pretty simple the speed increse is significant.
Also you can notice that the expression that evaluates as false is in 3rd position, so you may even get better performance when it is placed in 2nd or 1st position.

MAX = 20000
a = 1
b = 2
c = -3
d = 4

var i=MAX
while(--i > -1)
{
	if (a == 1 && b == 2 && c == 3 && d == 4)
	{
		var k = d * c * b * a
	}
}


var i=MAX
while(--i > -1)
{
	if (a == 1)
	{
		if (b == 2)
		{
			if (c == 3)
			{
				if (d == 4)
				{
					var k = d * c * b * a
				}
			}
		}
	}
}

( ... continues on page 3)


           
 
 
Name: Marco Lapi, a.k.a Lapo
Location: Fossano, Italy
Age: 34
Flash experience: started out with Flash 4 back in 1999
Job: web designer/developer
Website: http://www.gotoandplay.it/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048