views:

1235

answers:

5
+4  Q: 

Vector.<> vs array

What are the pros and contras of using a Vector.<> instead of array?

+7  A: 

From the adobe documentation page:

As a result of its restrictions, a Vector has two primary benefits over an Array instance whose elements are all instances of a single class:

  • Performance: array element access and iteration are much faster when using a Vector instance than when using an Array.
  • Type safety: in strict mode the compiler can identify data type errors such as assigning a value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector. Note, however, that when using the push() method or unshift() method to add values to a Vector, the arguments' data types are not checked at compile time but are checked at run time.
Arno
Thanks Arno! These are only the pros, are there also some contras?Concerning the better performance of Vector: some people report the contrary: see http://impossiblearts.com/blog/2008/06/18/fp10-vector-vs-array/comment-page-1/ and http://bugs.adobe.com/jira/browse/FP-1802
Ilya Boyandin
It certainly looks like performance might not be the best selling point for vectors right now. Then again, one should be already used to the fact that both the Flex framework sdks and the flash player are far from being bug free.
bug-a-lot
The big contras are in my opinion that it is very new so a little buggy and that not everyone has FP10. In this test of Mike Chambers: http://www.mikechambers.com/blog/2008/08/19/using-vectors-in-actionscript-3-and-flash-player-10/ in his test is not a hugh difference between the 2 numbers. If your application is not heavily hanging on big lists I would go with Array.
Arno
+1  A: 

Pro: Vector is faster than Array - e.g. see this: Faster JPEG Encoding with Flash Player 10

Contra: Vector requires FP10, and according to http://riastats.com/ some 20% of users are still using FP9

tst
A: 

According flash player penetration website it is a little higher. Around the 85%

This is the source

Arno
+2  A: 

Vectors are faster. Although for sequential iteration the fastest thing seems to be linked-lists.

Vectors can also be useful for bitmap operations (check out BitmapData.setVector, also BitmapData.lock and unlock).

A: 

The linked list example mentioned earlier in comments is incorrectly written though it skips odd nodes and because of that only iterates the half amount of the same data. No wonder he get so great results, might be faster with correct code as well, but not the same % difference. The loop sets current = current.next one time too much (both in the loop and as loop-condition) each iteration which cause that behavior.