Update 2 (Read first):
I noticed there are still some trackbacks to this post. To clear things a little bit up. This post is 2 1/2 years old and the Flash Player version used was one of the first (the first?) that supported Vector at all. The results of this tests are NOT VALID! any more. In general the tests are not completly wrong, but they do not fit into real-world scenarios.
Update
I have replaced the Sprite by an own class/object (SimpleObject with 3 public properties, x, y, z). The results are the same.
Vector vs Array
Started a little speed measurment with new Vector-type and the “old-fashioned” array. As everyone could read in the Adobe AS3 Docs, Vector should give us some advantages over Array iteration and data-access.
I have tried several test-settings and i was completely surprised by the results (in a bad way).
What i am most interested in, is fast access of objects. So i create a pool of object-references(simple sprite), one as an array and the other pool is defined as a Vector.<Sprite>.
[as]
var i:uint = 0;
var n:uint = N_TEST; ( n = 20000000)
var v:Vector.<Sprite>;
v = this.objectPoolV;
var timestamp:int = getTimer();
i = 0;
var r:Sprite;
while (i < n)
{
r = v[i];
i++;
}
result.htmlText += “<br />Result Vector Complex Operation Test: ” + (getTimer() – timestamp);
[/as]
This was my first test and the difference is noticeable (but look at n, how high it has to be for even get a small difference).
- Vector result: 511 ms
- Array result: 659 ms
But this is not a very commen use. So, next step was to save the reference in a locale variable and move the sprite in all 3 directions about 10px. I have done this test with and without an explicit cast.
[as]
var i:uint = 0;
var n:uint = N_TEST;
var v:Array;
var r:Sprite;
v = this.objectPoolA;
i = 0;
var timestamp:int = getTimer();
while (i < n)
{
r = v[i];
r.x += 100;
r.y += 100;
r.z += 100;
i++;
}
result.htmlText += “<br />Result Array Complex Operation Test: ” + (getTimer() – timestamp);
[/as]
This result i have never expected, array is faster!! than the vector, but why? I am only referencing to the current dataset.
- Vector result: 5118 ms
- Array result: 4885 ms
If i cast the array, the result is worse than without a cast.
[as] r = Sprite(v[i]); [/as]
I know, casting is expensive, but look at the results with casting.
- Vector result: 7055 ms
- Array result: 7126 ms
So, finally let’s have a look at the worst sample. Direct access of properties threw the Array and Vector.
[as]
var i:uint = 0;
var n:uint = N_TEST;
var v:Vector.<Sprite>;
v = this.objectPoolV;
i = 0;
var timestamp:int = getTimer();
while (i < n)
{
v[i].x += 100;
v[i].y += 100;
v[i].z += 100;
i++;
}
result.htmlText += “<br />Result Vector Complex Operation Test: ” + (getTimer() – timestamp);
[/as]
I have to reduce the N_TEST constant to 1000000, here are the results
- Vector result: 1751 ms
- Array result: 1631 ms
Is anyone able to confirm this results?
The complete testfile is downloadable here. (If you find a mistake, please tell me).


