Posted by: Yogesh Puri on: July 17, 2008
I have been exploring the ways to copy an Array object to a new Array with less of the effort. Reason being the following statements in ActionScript
var arrayA:Array = new Array(“Jack”,”Steve”,”Mark”,”Ted”);
var arrayB:Array = arrayA;
The above statements appears to create a copy of arrayA, however in the actual it creates a new reference to the arrayA object. The problem is that if you execute any operation on arrayB object now it will impact the arrayA eventually. For example:
arrayB.pop();
trace(arrayA);
The statement is supposed to trace “Jack,Steve,Mark,Ted” whereas it will trace “Jack,Steve,Mark”.
To overcome this problem we use the following methodology
var arrayA:Array = new Array(“Jack”,”Steve”,”Mark”,”Ted”);
var arrayB:Array = new Array();
for(var i:int = 0; i < arrayA.length; i++) {
arrayB[i] = arrayA[i];
}
arrayB.pop();
trace(arrayA);
Now it will trace the desired result i.e. “Jack,Steve,Mark,Ted” and if you trace arrayB then it will trace “jack,Steve,Mark”
This methodology is very good when you are dealing with single array objects but when it comes to Nested Arrays it is really a tedious job as you have to look for every inner object of an array if it is an Array type and then again create a new array and copy it’s element. To overcome this we can use now the ByteArray class of Flash.utils package
You can review the following code used to copy an array. There is a disadvantages of using the ByteArray and that is you will loose on some speed. The ByteArray.Read method is slower than copying an Array. You can see a benchmark here
——————————–
Copying a 100000 elements array
——————————–
Copy with loops starts at :: 802504163
Copy with loop ends at :: 802504210
Time elapsed :: 47 milliseconds
Copy with ByteArray Starts at :: 802504257
Copy with Byte Array :: 802504382
Time elapsed :: 125 milliseconds
There is certainly a significant amount of processing time which ByteArray take however it is eventually helping us out when it comes to copying nested objects.
Download the fla from here.
Here is the code for the same.
——————————————————–
——————————————————–
import flash.utils.ByteArray;
var tDate:Date = new Date();
var iStartTime:uint;
var iTime:uint;
var byteArray:ByteArray = new ByteArray();trace(“——————————–”);
trace(“Copying a 100000 elements array “);
trace(“——————————–”);
//———- Create and Populate Array ———
var arrayA:Array = new Array();
for (var i:uint; i<=100000; i++) {
arrayA.push(Math.random()*i);
if (i == 100000) {
copyArray();
}
}//———– Copy Array using Loops ——–
function copyArray():void {
tDate = new Date();
iStartTime = tDate.time;
trace(“Copy with loops starts at :: ” + iStartTime);
var arrayB:Array = new Array();
var arr_len:uint = arrayA.length -1;
for (var i:int = 0; i <= arr_len; i++) {
arrayB[i] = arrayA[i];
if (i == arr_len) {
tDate = new Date();
iTime = tDate.time;
trace(“Copy with loop ends at :: ” + iTime);
trace(“Time elapsed :: ” + (iTime – iStartTime) + ” milliseconds”);
copyWithByteArray();
}
}
}
//——- Function copy with Byte Array ———
function copyWithByteArray():void {
tDate = new Date();
iTime = tDate.time;
trace(“Copy with ByteArray Starts at :: ” + iTime);
byteArray.writeObject(arrayA);
byteArray.position = 0;
var arrayB:Array = byteArray.readObject() as Array;
tDate = new Date();
var iByteArrTime:uint = tDate.time;
trace(“Copy with Byte Array :: ” + iByteArrTime);
trace(“Time elapsed :: ” + (iByteArrTime – iTime) + ” milliseconds”);
}
ByteArray is slower because, it has to serialize the array into string like structure, and then deserialize it.
If we had C-like mem-copy function, It could have been done in the blink of the eye.
Can this approcah be used to store a multidimensional array in a database and then extract the data and reconstitute the array at a later time?
Not sure how to approach this.
Thanks,
Kit
Hello,
I think you could simply use :
var arrayA:Array = new Array(“a”, “b”, “c” );
var arrayB:Array = array.slice();
it is faster and create a copy (not a reference) of arrayA in ArrayB.
But now, in order to gain speed, we just have to use the new Vector native object…
Hi Yogesh,
You are right. You can use bytearray only while a complex object cloning (deep cloning). Otherwise native copy utilities are okay enough to work.
Hi Shawn,
As Yogesh wrote earlier, you can use like this.
var ba:ByteArray = new ByteArray();
ba.writeObject(yourobject); //The place where byte cloning occurs.
//Now read it as…
var obj:*= ba.readObject();
Thanks,
Pravin
I want to pass the bytearray from Flash to .Net.
Can you help me.
I want to pass the byte array from flash to .Net.
Kindly provide me the way to resolve this.
Thanks
July 25, 2008 at 2:04 pm
it more simple that
var arrayA:Array = new Array(“Jack”,”Steve”,”Mark”,”Ted”);
var arrayB:Array = arrayA.concat();
arrayB.pop();
trace(arrayA);
trace(arrayB);
no?