Problem
Using delete
on an array element to remove it from the array is the only way I'm aware of to remove an element from an array such that a .forEach()
call skips the index.
Questions
- Does using the
delete
on an index,exampleArray[i]
for example, cause a subsequentexampleArray.push()
to increase the memory consumption of the array object? How does deleteing an object effect the garbage collector?
Is there a more efficient way of ridding an
exampleArray
of an element?
Example of the former
var exampleArray = [];
var n = 500;
//Does this line imply a memory allocation?
exampleArray.length = n;
exampleArray.fill("Lorem Ipsum", 0);
exampleArray.forEach(function(cur, ind, arr) {
if(ind % 4 === 0) {
delete arr[ind]; //Actually deletes the object itself, index no longer exists
//Length does not change, however. Does available memory?
}
}, this);
n /= 4;
//Where, in memory, are these placed?
while(n--) exampleArray.push("amet dolor");
Any help is appreciated, thank you.
delete
is not the only way to remove an index from an array, in fact, it doesn't do that at all, and isn't even intended for arrays ? How it affects the garbage collector, and anything else, really becomes irrelevant after that ?