小编And*_*son的帖子

在数组上使用delete和随后的.push()会影响性能/内存消耗吗?

问题

使用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)

javascript arrays micro-optimization

6
推荐指数
1
解决办法
119
查看次数

标签 统计

arrays ×1

javascript ×1

micro-optimization ×1