Removing Javascript Array for Garbage Collection

I have an array of javascript objects, each of which is created using "new". In case of an error, I would like to clear the entire array so that it is a GC'ed JS engine. To this end, is it enough to just set the array variable to "null" or do I need to combine all the elements from the array and set them to null before setting the array variable to "null"?

The reason I ask is because in Firefox, which I displayed (console.log), the array before assigning it to zero, and the display object (which is usually updated on the display, which I assume, even later), still showing the elements of the array when I inspect it later, so I doubt that the elements are actually free or not.

TIA

+4
source share
1 answer

To clear the array, you can simply set the length to zero:

var arr = [1,2,3,4,5]; console.log(arr); arr.length=0; console.log(arr); 

Result:

 [1, 2, 3, 4, 5] [] 

Edit: Just found this on the topic: http://davidwalsh.name/empty-array

Looking at the comments, it seems that just setting the variable to a new array is the easiest method:

 arr = []; 

According to the test results, GC'd memory is faster than setting the length to 0. I assume this is due to the distribution starting GC.

There is an interesting performance test for various methods here: http://jsperf.com/array-destroy

+5
source

Source: https://habr.com/ru/post/1492906/


All Articles