10) Nested
loops (for vs. while)
Nested loops are a common programming technique to access 2 dimensional
data, like map arrays in a tile based game, etc...
As you may expect from the previous tests the while
loop performs better.
11) TellTarget vs. dot syntax
If you started out with Flash before version 5, you will remember
the way TellTarget
was used to command moviclips.
Well, even if it was deprecated by Flash 5 and subsequent versions
in favor of the more elegant “dot syntax”, it looks
like the old fashioned Tell Target does his job better than anyone
else. So you may consider “downgrading” some of you
scripts to achieve an extra bit of speed.
Check this code snippet:
MAX = 10000
mc = _root.createEmptyMovieClip("test", 1000)
function test_dot()
{
var i=MAX
while(--i > -1)
{
mc._x = 10
mc._y = 10
}
}
function test_tellTarget()
{
var i=MAX
while(--i > -1)
{
tellTarget(mc)
{
_x = 10
_y = 10
}
}
}
12) Accessing local vars (This vs. With vs. Reference)
Local variables and objects can be referenced in more than one way.
We've tested two ways to do it and found out that the usage of the
keyword this produces the worst results.
More speed can be achieved using with operator.
The following code was used as a test, running a 10000 iteration
loop
obj = {}
obj.a = 1
obj.b = 2
obj.c = 3
obj.d = 4
obj.e = 5
obj.f = 6
obj.g = 7
obj.h = 8
obj.test1 = useThis
obj.test2 = useWith
MAX = 10000
function useThis()
{
var i = MAX
while(--i > -1)
{
this.a = 1
this.b = 2
this.c = 3
this.d = 4
this.e = 5
this.f = 6
this.g = 7
this.h = 8
}
}
function useWith()
{
var i = MAX
while(--i > -1)
{
with(this)
{
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
}
}
}
13) Loop listening for pressed keys
Listening for key presses can be optimized the same way we did with the Math
functions name lookup.
Instead of using if(Key.isDown(Key.LEFT)) every time it is much better to setup
a series of references like this:
keyDown = Key.isDown
keyLeft = Key.LEFT
and then use if (keyDown(keyLeft))
We also tested the usage of key code numbers instead of key code constants
but differences are not significant.
14) Math.floor() vs int()
This tip was submitted by iopred in the Flashkit forums (http://www.flashkit.com/board/showthread.php?s=38baf94bb869a0376c6f599d8bf2685d&threadid=434875)
Instead of using the new Math library floor() function try the old-fashioned
int()
Our benchmarks show a nice speed increase.
15) Eval vs. array access
We did not find significant performance boost using the eval() function over
the
array-like indexing introduced by Flash MX.
In other words this code:
var mc = eval("_root.myMc" + i)
doesn't seem to be much faster than
var mc = _root["myMc" + i]
As you can see from our chart the difference is really small.
16) Looping through movieclips: ASBroadcaster vs. Loop
This test shows how the undocumented Flash MX ASBroadcaster object
can help
in squeezing out some extra speed out of the flash player.
The benchmark creates 500 movieclips and then it tests how much
time it is needed to remove them.
The looping version calls the removeMovieClip() on each mc while
the ASBroadcaster version subscribes each clip to a custom event
handler. When the event broadcaster dispatches the custom event
all movicelips handle that event and this results in a much better
performance.
This is the looping version:
MAX = 500
SX = 550
SY = 330
MovieClip.prototype.onCustomEvent = function()
{
this.removeMovieClip()
}
function init()
{
var i=MAX
var rnd = Math.random
while(--i > -1)
{
var m = _root.attachMovie("enemy","e"+i,i)
m._x = rnd() * SX
m._y = rnd() * SY
}
}
init()
function bench()
{
var t = getTimer()
var i=MAX
while(--i > -1)
{
_root["e"+i].onCustomEvent()
}
res.text = "time: " + (getTimer() - t)
}
The same thing, using ASBroadcaster:
MAX = 500
evtManager = {}
ASBroadcaster.initialize(evtManager)
SX = 550
SY = 330
MovieClip.prototype.onCustomEvent = function()
{
this.removeMovieClip()
}
function init()
{
var i=MAX
var rnd = Math.random
while(--i > -1)
{
var m = _root.attachMovie("enemy","e"+i,i)
m._x = rnd() * SX
m._y = rnd() * SY
evtManager.addListener(m)
}
}
init()
function bench()
{
var t = getTimer()
evtManager.broadcastMessage("onCustomEvent")
res.text = "time: " + (getTimer() - t)
}
Compiling for flash player 6 using Flash MX 2004:
One of the interesting things with FMX 2004 is that the actionscript
compiler has been improved under many aspects.
The new compiler does a better job with both global (player 6 and
7) and local register (player 7 only) making some of the tweaking
presented here less important.
You can see by yourself, from our benchmark table, that some very
interesting results were achieved.
The most noticeable improvements we noticed were that _global variables
seems to be a lot faster than timeline variables and nested loops
now perform way better than before.
Also you will notice that almost every test run faster than the
Flash-MX compiled version.
Conclusions:
What we can learn from all these benchmarks is that by accurately
choosing the right way to code certain time critical functions,
we can greatly improve the flash player performance.
Although we've presented here a good number of optimization tips,
there's much more for you to experiment with and more benchmarks
could be created to show other ways of code tweaking.
We feel like the best approach is to isolate those very cpu-intense
routines and try to apply as many of these advices as possible and
also running your own tests for your specific problems.
If you wish you can furtherly discuss these topics in our forums
by posting in this thread
Other code-optimization resources:
http://flasm.sourceforge.net/
Flasm is a nice utility that can compile and decompile actionscript
in its assembler-like form, so you can directly manipulate the SWF
bytecode. It works also with Flash MX 2004.
On the website you can find optimization tips, an insight into flash
byetcode and you can also download the Flash sourcecode.
The
Flashkit Board Optimization thread
A very long thread discussing many actionscript optimization tips.
A huge resource of examples!
Bit-101
Forum Optimizations Tips
Another interesting discussion about code tweaking.
OddHammer
Tips
28 quick tips for optimizing yor AS code.
|