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

- 10,241
- 14
- 58
- 78

- 3,069
- 25
- 23
-
As a side note, this should be tagged flex4 flashplayer10. – bug-a-lot Jul 16 '09 at 11:30
5 Answers
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.

- 514
- 5
- 13
-
3Thanks 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 Jul 15 '09 at 10:14
-
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 Jul 16 '09 at 11:50
-
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 Jul 16 '09 at 12:44
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

- 3,321
- 1
- 16
- 7
-
At the time of this comment, the non-detected version + flash 9 clocks in at 4% – ADB Oct 24 '11 at 19:08
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).

- 81
- 1
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.