ActionScript 3 Vector / Array Performance Comparison
In my original post on the new Flash Player 10 Vector class, I did a simple example that showed Vectors being slightly faster than Array when just populating and looping through collections.
Below is another example that shows a more significant performance increase when using Vectors. In this example, I populate an Array and Vector with 5 million random numbers, and then loop through them and average all of the numbers.
Running the test on my MacBook Pro, the Vector is about 60% faster:
Vector 1.824
Array 2.938
62.08 %
Here is the code:
package {
import flash.display.Sprite;
import __AS3__.vec.Vector;
public class VectorPerformance extends Sprite
{
private static const LENGTH:int = 5000000;
public function VectorPerformance()
{
//vector performance
var vectorStartTime:Number = getTime();
var v:Vector.<Number> = generateVector();
var vAvg:Number = averageVector(v);
var vectorTime:Number = (getTime() - vectorStartTime) / 1000;
//array performance
var arrayStartTime:Number = getTime();
var a:Array = generateArray();
var aAvg:Number = averageArray(a);
var arrayTime:Number = (getTime() - arrayStartTime) / 1000;
trace("Vector", vectorTime);
trace("Array", arrayTime);
trace((vectorTime/arrayTime) * 100 + " %");
}
private function generateVector():Vector.<Number>
{
var v:Vector.<Number> = new Vector.<Number>(LENGTH, true);
for(var i:int = 0; i < LENGTH; i++)
{
v[i] = Math.random() * 100000;
}
return v;
}
private function averageVector(v:Vector.<Number>):Number
{
var sum:Number = 0;
var len:int = v.length;
for(var i:int = 0; i < LENGTH; i++)
{
sum += v[i];
}
return (sum / len);
}
private function generateArray():Array
{
var a:Array = new Array(LENGTH);
for(var i:int = 0; i < LENGTH; i++)
{
a[i] = Math.random() * 100000;
}
return a;
}
private function averageArray(arr:Array):Number
{
var sum:Number = 0;
var len:int = arr.length;
for(var i:int = 0; i < LENGTH; i++)
{
sum += arr[i];
}
return (sum / len);
}
private function getTime():Number
{
return (new Date()).getTime();
}
}
}
Of course, this is only one test. Depending on what you are doing, Vectors may be even faster, or slower.
One interesting note, originally my test had each collection sorted before they were averaged, but it turned out that sorting Vector was slower than sorting the Array. Not sure if this is a bug, but I have reported it to the player team.






40% faster :)
magicwind
24 Sep 08 at 6:27 pm
[...] Chambers has posted a comparison between the performance of AS3 arrays and the new Vectors available in Flash Player 10. The results [...]
Actionscript 3 vector speedups » CleVR » Panoramic photography and image stitching blog
25 Sep 08 at 2:02 am
How generics used with vector affects the test?
radekg
25 Sep 08 at 11:59 am
[...] Aqui el codigo fuente [...]
Hidden Place Blog » Blog Archive » Comparacion de rendimiento entre Vector y Array en AS3
26 Sep 08 at 6:25 am
Why my vector use source is need more memory?
private var vector:Vector. = new Vector.;
private var array:Array = new Array();
private function init():void
{
var start:Number = new Date().getTime();
for(var i:Number = 0 ; i < 5000000 ; i++)
{
array[i] = Math.random() * Math.PI * 100000;
//vector[i] = Math.random() * Math.PI * 100000;
}
trace(System.totalMemory);
trace(new Date().getTime() – start);
}
Vector
71843840
2782
Array
15237120
2859
mash
30 Sep 08 at 10:40 pm
[...] reports have been coming in across the web of performance increase anywhere between 40% to 60% (AS3 Vector / Array Performance Comparison). However, this is one small step for the platform as a whole as with some of the benchmarks I have [...]
Beyond Flash 10 and AS3 | JADBOX: Web Application Musings
3 Oct 08 at 7:43 am
Are there built-in comparators for sorting Vectors in Flash Player 10? I need to alphabetically sort a Vector based on the return value of a method.
TK
24 Oct 08 at 5:24 pm
[...] Vector is an interesting idea. It adds type-safe collections to ActionScript, and it provides a pretty dramatic speed increase since it side-steps things like type-checking. But Vector does not provide a generic typing [...]
Marking Occurrences » Listen to me rant about ActionScript, part 2
8 Dec 08 at 11:22 pm
Just ran across an interesting performance comparison tool that shows Vectors as 60x faster when looping through them.
http://businessintelligence.me/projects/performance_tester/performanceTester.html
PS: Actio script?
Mims H. Wright
17 Dec 08 at 11:40 am
[...] ActioScript 3 Vector / Array Performance Comparison [...]
AS3 Vector vs Array Speed Test « Strings And Wires
15 Feb 09 at 3:29 am
[...] with an Array which holds all the same DataType. Vectors are approximately 40% faster according to Mike Chambers to Arrays. When dealing with a large Array that is the same type, use a Vector. Also note that [...]
Cool Flash Tip of the Week #3 | kreativeKING - Interactive Developer
10 Jun 09 at 11:41 am
I did my own testing a little while ago and I got the following results:
With Number, int, and uint, Vector is blisteringly fast, coming in at 8.2 – 8.5 times faster for writing data in the fastest way, that is setting an index like:
vec[i] = i
or
arr[i] = i
However, splicing data from Arrays is FAR faster in Arrays than in Vectors.
The only way I could get vector to do this in under 15 seconds for a million iterations was:
vec.push(i)
vec.splice(0,1)
the two speeds recorded being 3743 and 3775 ms
This same functionality in array clocked in at about 970 – 1000 ms faster. Other methods, such as:
vec[i] = i
vec.splice(i,1)
(I set the length property to twice the number of iterations to prevent an error from being thrown)
Came in at over 15 seconds. Only after lowering the number of iterations to 10 thousand did it finish, with a time of 117 ms (it seems that it increases exponentially?). The same number of iterations and method yields 33 ms for an array.
It seems Vector..splice() needs to be looked at xD
Matt
5 Aug 09 at 9:24 pm
[...] While this example might not benefit greatly from such a process, there are circumstances that do benefit from it – using extensive 3D calculations or trig functions such as Sine and Cosine. Something else to consider would be to use Value Objects that have the values typed to them for faster access and then have these stored in a Vector. [...]
Optimizing Flash Animations Via Lookup Tables - Touch My Blog
13 Sep 09 at 4:33 pm
I think it would be interesting to know when to use Vector and when to use Array. Anyone find anything like that? Or do we just always write it both ways and simply pick the fastest?
Anthony
24 Oct 09 at 3:19 pm
An Array in AS3 is really a hashmap. A Vector is really an array (in the C sense).
The performance difference is the difference between calculating a hash on the index-key, and look the value up. For the Array (hashmap), this is non-trivial, it means looping over the bytes (or words) of the key, doing some operations. For the Vecotr (array) it is a simple lookup.
Your above code measures the performance of the random() function as much as the cost of access.
Averaging 5 million values in a Vector is measured in miliseconds.
Martin Høyer Kristiansen
6 Nov 09 at 4:24 am
[...] You may view further comparision of Array and Vector performance in Mike Chambers’ blog. [...]
Hemanth Sharma's Tech Blog
24 Nov 09 at 5:33 am
[...] child list, and with less bloat. New to Flash Player 10 is the Vector() class, and while they are faster than arrays, are they faster than getChildAt() and the child list? While being faced with a new particle-based [...]
ActionScript 3 Vector / getChildAt Performance Comparison • Steven Vachon's Blog
11 Dec 09 at 2:20 pm
Martin, this is not entirely true.
Array is a mix between HashTable and DenseArray.
Please read my Tamarin analysis at:
http://jpauclair.wordpress.com/2009/12/02/tamarin-part-i-as3-array/
jpauclair
15 Dec 09 at 4:37 pm
I have noticed projects missing the point that even Vectors need to be converted to an ArrayCollection prior to being bound to anything, so if you use Vector everywhere, you actually degrade performance as it adds an extra conversion.
jboyd
21 Apr 10 at 6:43 am
hey you know what else would make that test more optimized to compare the array vs vector….
++i
is faster than
i++
one giant media
27 Apr 10 at 2:22 am
[...] the end, the solution to this problem came in several forms. First, Flash 10 introduced a fixed-sized, strongly-typed Vector class which makes working with large arrays of data much faster. Next, I recently came across skip lists [...]
Process: Mapping War Logs for the Guardian
28 Jul 10 at 11:58 am