Best way to remove all elements from an ActionScript Array?

20,966

Solution 1

I'd say:

myArray = [ ];

That's explicit, short, and makes good use of the VM's garbage collector.

Your first alternative runs a lot of interpreted code to get the same result.

I don't know that the second does what you want; if it does, it's hacky, unclear.

The "new Array()" variant of the third alternative is just wordy, offering no advantage over an array literal. If you also write JS and use JSLint, you'll get yelled at for not using the array literal form.

Solution 2

I'm afraid to say but Warren Young is wrong when he said that setting the myArray = [] cause the garbage collector to pick up the array.

as you have the ability to add a reference to itself within itself, and therefore would never be collected and using up memory, especially if the array has some Sprites in the array, as they too would have the array references them and they too would never be collected.

Sly_cardinal and Richard Szalay are 100% correct. but the length parameter is not needed in Richard's.

To totally clear the array and make sure its collected by garbage then

myArray.splice(0);
myArray = null;

Solution 3

It depends on your context. While using Mr. Young's answer is often the most correct way to do things, it will not always work, especially if you have two variables pointing to the same array:

var foo:Array
var bar:Array
foo = bar = [ 1, 2, 3 ];
bar = [];
trace( foo ); // 1,2,3

On the other hand, if you actually empty the array manually:

var foo:Array
var bar:Array
foo = bar = [ 1, 2, 3 ];
var l:int = bar.length; // get the length FIRST! 
                        // Otherwise bar.length will change
                        // while you iterate!

for( var i:int = 0; i < l; i++ )
{
    bar.shift();
}
trace( foo ); // does not trace anything

Solution 4

If you can modify the array reference, then I would go with Warren's answer. If you need to modify the existing instance, you can also use Array.splice:

var arr : Array = [1, 2, 3, 4, 5];

arr.splice(0, arr.length);

Solution 5

According to this test the best way is to set length = 0

Share:
20,966

Related videos on Youtube

Chris R
Author by

Chris R

Co-Founder of hipchat.com - taking a deep dive into Flex and AIR.

Updated on July 09, 2022

Comments

  • Chris R
    Chris R almost 2 years

    I'm writing an application in Flex / ActionScript and have a number of class member variables of type Array storing data.

    My question is: what's the "best" way to clear out an Array object?

    I noticed the ArrayCollection class has a function removeAll() which does this, but the basic Array class does not. Some possibilities I've considered are:

    • Iterating through the array, calling pop or shift on each element
    • Setting the array length to 0
    • Setting the member variable to a "new Array()" or "[]"
  • Andy Li
    Andy Li over 14 years
    Set length = 0 is not hacky as the official as3 lang ref mentioned it: help.adobe.com/en_US/AS3LCR/Flash_10.0/Array.html#length. I personally use this method too. And I agree with Christopher W. Allen-Poole that your method is not clearing the array actually.
  • Brian Hodge
    Brian Hodge over 14 years
    while(bar.length > 0) bar.shift(); // :)
  • Sly_cardinal
    Sly_cardinal over 14 years
    bar.splice(0); // No loop needed :)
  • Alexander Farber
    Alexander Farber about 12 years
    arr.splice(0); or better: arr.length = 0;
  • Pup
    Pup about 10 years
    myArray.length = 0 is more unclear in the sense that it introduces a needlessly descriptive component of the array when all that should be conveyed to the reader is "delete array". The truncation feature of the length setter initially struck me as a kind of an added bonus. Anyway, Christopher W. Allen-Poole's (cwallenpoole) response makes a lot of sense and seems slightly more apparent since the "splice" operation is more literal and is designed specifically to remove items.

Related