How to dispose a very big array in js

javascript

Solution

If you remove all references to the array, any element of the array on which you don't keep a reference otherwise will be freed, that is the garbage collector will be able to reclaim its space.

You don't need to manually set to `null` the elements of the array. Your last loop is totally useless.

If you just want to reclaim the space of the array, not its elements, just remove the reference to it, by doing

array = undefined; 

If your array isn't stored in a variable but in a property, for example defined as `window.array = []`, then you can also use `delete` :

delete window.array; // or yourObject.array

But you'll still have to delete all reference to that array.

Problem

I have an array of thousands of simple objects, and I want to dispose it. example: ``` var array = [ {name:"1"}, {name:"2"}, ..., {name:"32,000"} ] ``` And I want to dispose only the array (I have references to the objects somewhere else). So, is the next example enough to kill the array? example: ``` var a1 = {name:"1"}, a2 = {name:"2"}, ... , a32000 = {name:"32,000"}; var array = [ a1, a2, ... , a32000 ]; array = null; ``` Or should I do something like: ``` for(var index in array) array[index] = null; array = null; ```

Original source