How to get rid of a very large array in js

I have an array of thousands of simple objects and I want to destroy it.

Example:

var array = [
   {name:"1"}, {name:"2"}, ..., {name:"32,000"}
]

      

And I only want to use an array (I have object references somewhere else). So is the following 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 do I need to do something like:

for(var index in array) array[index] = null;
array = null;

      

+3


source to share


1 answer


If you remove all references to the array, any element of the array where you don't store the reference otherwise will be freed, meaning the garbage collector can reclaim its space.

You don't need to manually set to null

array elements. Your last cycle is completely useless.

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

array = undefined; 

      



If your array is not stored in a variable, but in a property, for example defined as window.array = []

, you can also use delete

:

delete window.array; // or yourObject.array

      

But you still have to remove all reference to that array.

+6


source







All Articles