问题
使用delete数组元素将其从数组中删除是我意识到从数组中删除元素以使.forEach()调用跳过索引的唯一方法.
问题
deleteon索引exampleArray[i]会导致后续exampleArray.push()增加数组对象的内存消耗吗?删除对象如何影响垃圾收集器?
是否有更有效的方法来消除exampleArray元素?
前者的例子
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 …Run Code Online (Sandbox Code Playgroud)