Are javascript objects inside an array removed from memory when the array is cleared?

I never thought about garbage collection, and I don’t know whether to take into account when creating small javascript games / applications. Any advice is appreciated, but I will ask my specific question at the end.

Many times I write code of this form:

var foos=new Array();
generateFoos();
function generateFoos()
{
    foos=[];
    for (fooIndex=0;fooIndex<numberOfFoos;fooIndex++)
    {
        foos[fooIndex]=new foo(Math.random(),Math.random());
    }
} 
function foo(bar,bas)
{
   this.bar=bar;
   this.bas=bas;
}

So my question is: when I say foos=[](line 5), does it delete objects from this array from memory or floats somewhere, making the program larger and slower? What if I want to call generateFoos()loooot once, for example, every time the user presses a key.

Thank!

+3
source share
2 answers

, , , , foo = [] .

: " , ". , .

, .

, . .

.

1) , ,

2) . , , . , , . .

3) . , var, . var , . , .

4) var . .

5) . . 20- , .

6) var. , . .

:

var numberOfFoos = 10,
    foos = [];

var generateFoos = function(){

    foos = [];

    for( var fooIndex = 0; fooIndex < numberOfFoos; fooIndex++ ){

        foos[ fooIndex ] = new foo( Math.random(), Math.random() );

    }

},

foo = function( bar, bas ){

   this.bar = bar;
   this.bas = bas;

}

generateFoos();
console.log( foos );
+8

: , . , .

, .

. (, ). .

+6

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


All Articles