Actionscript 3 sorting performance

Recently I decided to look more into performance and optimization of Actionscript 3/Flash, mostly to learn what to do and what not when writing AS3 code.

A quick Google search gave me only few, outdated and not very reliable articles, so I decided to write few tests and to share the findings on my blog.

The tests

In this post I’m going to share the results of a first set of benchmarks aimed at testing the performance of several sorting functions available in AS3 for Arrays and Vectors.

The benchmarks are divided in 3 main groups, based on the data placed in the containers:

int

String

object

All the tests are ran on 3 different containers:

Array

Vector

Fixed size Vector

each container is filled with 50,000 randomly generated elements.

For all the tests times are in ms. and they are the average of 5 runs (spikes are not considered in the average), lower values are better.

The environment

The tests were executed on a 64-bit Windows 7 machine powered by an Intel i7-4770 CPU and 8Gb of DDR3 RAM.

The SWF has been generated with Flashdevelop 4.5.2 using Flex 4.6.0, obviously the configuration used to build is “Release”.

The Flash player used to run the tests is the latest available at the time of writing this post: 11,9,900,170.

Test group 1: int

For these tests the containers are filled with randomly generated ints.

CompareInt and CompareDescInt are two functions passed to sort() to compare two ints, the former is used for sorting them in ascending order, whereas the latter for sorting them in descending order.

Array Vector Vector (fixed) sort() 426 – – sort(Array.NUMERIC) 8 27 33 sort(CompareInt) 49 68 52 sort(Array.DESCENDING) 418 421 463 sort(CompareDescInt) 49 50 53

Few things emerge from these data:

Array is the best container for sorting ints.

The fastest way to sort ints is using sort(Array.NUMERIC).

Using a custom function to sort ints is very slow (6.25 times slower than the fastest sort).

NEVER use sort(Array.DESCENDING) to sort ints in a descending order, always sort the container and then call reverse() on it (which on average took 0 ms. on my data).

NEVER use sort() with ints, something stupid is happening when you do that.

Test group 2: String

For these tests the containers are filled with randomly generated Strings of 16 characters.

CompareStr and CompareDescStr are two functions passed to sort() to compare two Strings, the former is used for sorting them in ascending order, whereas the latter for sorting them in descending order.

Array Vector Vector (fixed) sort() 55 – – sort(Array.CASEINSENSITIVE) 433 467 442 sort(CompareStr) 58 58 59 sort(Array.DESCENDING) 29 29 30 sort(CompareDescStr) 80 80 80

Few things emerge from these data:

Array is the best container for sorting Strings.

Even if it’s hard to believe, apparently the fastest way to sort Strings is calling sort(Array.DESCENDING) and then reverse().

Do not use sort(Array.CASEINSENSITIVE) if not really needed.

Using a custom function to sort Strings is pretty slow (2 times slower than the fastest sort)

Test group 3: Object

For these tests the containers are filled with (relatively big) objects containing each: 11 ints, 1 Number, 1 String, 1 Shape and few functions.

CompareObj and CompareDescObj are two functions passed to sort() to compare an int value of two objects, the former is used for sorting them in ascending order, whereas the latter for sorting them in descending order.

Array Vector Vector (fixed) sort(CompareObj) 159 158 185 sort(CompareDescObj) 157 157 158 sortOn(int) 13 – – sortOn(String) 34 – – sortOn(int, String) 139 – –

Few things emerge from these data:

Array is the best container for sorting objects.

When sorting objects always use sortOn(…) to get the best results.

Using a custom function to sort objects is extremely slow (12.23 times slower than the fastest sort)

Conclusions

It’s clear that when you need to sort many elements your container of choice is the Array and that (not surprisingly) sorting ints is faster than sorting Strings or bigger objects, but using sortOn makes possible to obtain good performance with any kind of data.

Live Demo

If you want to run the benchmarks on your machine you can open this page.

Feel free to post your results as comment to this post.

Source Code

The whole Flashdevelop project is available as ZIP archive and can be downloaded here.

The source code is released under the “do whatever you want” license 😉 attribution and a link to this post would be appreciated though.