Been having an interesting discussion on the papervision3d channel on freenode. (irc.freenode.net #papervision3d) The question asked by a member was: “What’s the fastest way to copy an array in flash as3?” So Tom Richardson and I made some tests for fun and profit (some guys bet a beer and pizza to Tom that a simple for() loop would win the race) so I started to make a small and barebones test. Obviously knowing we were getting the goods ; we set out to prove them wrong!
We were evaluating some small snippets of code:
- The basic for loop with push calls
- The basic while loop
- ByteArray copying/cloning
- Optimized for loop
- slice()
- concat()
We took an array of 500000 elements (all random floats) as source and went for a copy, and here are the results!
List of 500000 elements.
for() unoptimized: 118ms
for() key-key optimized: 30ms
while() key-key optimized: 30ms
Bytearray copy: 176ms
Array slice(): 19ms
Array concat(): 8ms
The obvious big time winner seems to be using concat()! Less function calls, less evaluations (and hey less code!) Extremely elegant as well
Billy owes us pizza and beer.
Interestingly though, I was most surprised by the slowness of the unoptimized for() loop, scoring just over 100ms, compared to 180ms to perform a complete deep copy (bytearray)…that’s pretty poor. I guess developers would really gain some outstanding performance simply by optimizing their for() loops.
Using concat() also wins over slice() by about 10ms, which I’m not entirely sure why. I guess there might be addition function calls inside the language. Anybody got some insights on this?
Here is a small snippet demonstrating it and especially proving the copy array isn’t a reference:
Here is the source of the tests! Please don’t be afraid to kick and whine if something is not right. I’m also very interested in seeing if there would be an even FASTER way!


















August 4th, 2008 at 9:33 pm
It’d be interesting to see performance tests for copying a multidimensional array - recursive loops vs bytearray copy.
September 5th, 2008 at 11:08 am
[...] Flash AS3 optimization - Fastest way to copy an array [...]
October 4th, 2008 at 9:23 am
[...] http://agit8.turbulent.ca/bwp/2008/08/04/flash-as3-optimization-fastest-way-to-copy-an-array/ [...]
January 23rd, 2009 at 8:28 am
[...] La manera más rápida de copiar un array http://agit8.turbulent.ca/bwp/2008/08/04/flash-as3-optimization-fastest-way-to-copy-an-array/ [...]
February 10th, 2009 at 5:18 pm
[...] information about this and the source code that he used to find this out can be found here. RSS Filed under: AS3 Flash Shortcut Keys [...]
February 13th, 2009 at 8:45 am
There is a much faster way to loop:
Dont declare the counter var in the loop conditions.
Its about 50% faster.
//TEST-2b Barebones for loop, optimized
var t2sb:Number = getTimer();
var copy2b:Array = [];
var l2b:int = list.length;
var mb:int;
for(mb = 0; mb < l2b; mb++)
{
copy2[mb] = list[mb];
}
var t2eb:Number = getTimer();
trace(”for() key-key optimized b: “+(t2eb-t2sb)+”ms”);
February 13th, 2009 at 12:08 pm
I disagree. According to my tests, declaring the loop counter in the condition doesn’t affect the outcome. I added a 7th test to the list that doesn’t declare the loop counter in the condition and the time output between test 2 and test 7 were almost always very close to the same (varying from 1 to 2 ms each)
List of 500000 elements.
1. for() unoptimized: 144ms
2. for() key-key optimized: 35ms
3. while() key-key optimized: 35ms
4. Bytearray copy: 205ms
5. Array slice(): 24ms
6. Array concat(): 10ms
7. for() key-key optimized again: 36ms
You can repeat this test many tests and sometimes test 2 is faster by 1-2 ms, sometimes test 7 is faster by 1-2 ms. Overall, this proves to me that declaring the loop counter in the condition or outside has absolutely no effect on the performance of the loop.
You can download the updated test file here and see for yourself:
http://agit8.turbulent.ca/source/array_copy_speed.fla
February 22nd, 2009 at 11:33 pm
Well I’m with Benoit on this one, from what I can see declaring the loop counter inside or outside seems to bring a neglectable perf difference.
March 10th, 2009 at 9:19 pm
[...] information and the source code can be found here:Flash AS3 optimization - Fastest way to copy an array [...]
April 7th, 2009 at 4:22 pm
In the concat() case - what happens if you try to modify the array after copying? I wonder if it is really copying the data or just pointing to the other Array.
April 7th, 2009 at 4:39 pm
Hi joe, it’s copy not a pointer
That’s the whole point!
So if you modify the copy, it’s not modifying the original
-b
April 7th, 2009 at 4:52 pm
that is my question — if underneath they are really just sticking the original array on the end *until* you modify it. It’s called “copy-on-write” and it’s a pretty common technique to speed things up.
April 7th, 2009 at 4:53 pm
In that case — you would see a lag the first time you try to modify the new;y concat’d Array. If you don’t see a lag - that means that a true copy was made.
April 25th, 2009 at 12:34 am
[...] Fastest way to copy an array http://agit8.turbulent.ca/bwp/2008/08/04/flash-as3-optimization-fastest-way-to-copy-an-array/ [...]
April 25th, 2009 at 12:34 am
[...] Fastest way to copy an array http://agit8.turbulent.ca/bwp/2008/08/04/flash-as3-optimization-fastest-way-to-copy-an-array/ [...]
June 21st, 2009 at 6:10 pm
[...] Fastest way to copy an array http://agit8.turbulent.ca/bwp/2008/08/04/flash-as3-optimization-fastest-way-to-copy-an-array/ [...]