ActionScript performance #Arrays

As the Flash community rapidly walks towards the mobile devices, we especially need to think about how well our code performs.

On a mobile device, the hardware specs are much lower than on a regular desktop pc, hence, for example, an application must only use as much memory as it really needs. Furthermore one needs to concern more about Garbage Collection.
However, memory is not the only thing to worry about. On a mobile device, the processing time of the code can have serious effects on the users experience; some milliseconds can really make the difference!

Thank the universe, we have got hardware acceleration and an awesome caching system to render resource intensive graphics and movements on the GPU – that is an enormous relief for the ‘little’ CPU. Otherwise, Flash on a mobile device would really be a disappointing experience.

Now let us talk about the little things you can tweak while coding, which make up most of the programmatic speed of your application.

#Arrays

An Array is a class, right? A built in ActionScript 3.0 class. So if you want to create and use an Array, the basic idea is to instantiate an instance of the Array class like this:

var list:Array = new Array();

Good so far, but that solution is the slowest one to get an Array created.
From now on, you want to use this way to get an Array created:

var list:Array = [];

Performance tests

So I set up some performance tests on these two different approaches of creating an array, using Grant Skinner’s Performance Test Harness.

(tested on a Mac Pro 2 x 2.8 GHz Quad-Core with 6 GB DDR2 RAM – Flash Player 10.1.51.66 debug)

Iterations list = new Array(); list = [];
50.000 101 ms 52.75 ms
100.000 203.5 ms 103.5 ms
1.000.000 2012.25 ms 1037.25 ms

You can download the source files of the test. (Remember to link to the Performance Test class)

Conclusion

Using the bracket syntax to create an Array in ActionScript 3 is about 48% faster than using the usual new Array(); constructor syntax. So stick with this solution:

var list:Array = [];

Next time, in the ActionScript Performance series, I will talk about loops and how they perform with different syntax approaches.

Tags: , ,

2 Responses to “ActionScript performance #Arrays”

  1. teeth whitening says:

    The most comprehensive info I have found on this subject on the net. Will be back soon to follow up.

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>