gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:14:55 25 4
gpt4 key购买 nike

问题

在数组元素上使用 delete 将其从数组中删除是我知道的从数组中删除元素的唯一方法,例如 .forEach() 调用跳过索引。

问题

  • 是否在索引上使用delete,例如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 these placed?
while(n--) exampleArray.push("amet dolor");

感谢任何帮助,谢谢。

最佳答案

Does using the delete on an index, exampleArray[i] for example, cause a subsequent exampleArray.push() to increase the memory consumption of the array object?

push 会增加内存消耗,无论它之前是否有 delete。通常。如果引擎为额外的项目预先分配了存储空间,也许它不会。如果您认为引擎可能会以某种方式重新使用由 delete 打开的空间以避免在下一次 push 上分配额外的内存,很可能不会.

How does deleting an element effect the garbage collector?

如果删除的元素不在范围内,则它会受到 GC 的约束。

Is there a more efficient way of ridding an exampleArray of an element?

您必须决定是否介意以稀疏数组结尾。如果您不这样做,并且正如您指出的那样,forEach 等跳过这些漏洞,那么 delete 是最快的。如果您想压缩已删除元素周围的数组,例如使用 splice,成本可能会高出 2 倍。

引擎在内部实现不同的数组表示策略,有时会在它们之间切换——例如,当数组达到特定的稀疏度时。每个引擎都有不同的策略。回答这类性能问题的唯一可靠方法是运行性能测试,或阅读引擎源代码。

他们关于 GC 的关键点是您不必担心它。您不想对引擎进行事后猜测。您可以优化一个引擎,然后发现另一个引擎的性能变差了。

所有这些微优化问题只有在您的逻辑涉及要在其上执行数百万个运算符的庞大数据对象时才有意义。如果是这种情况,您可能想要推出自己的数据结构。

关于javascript - 在数组上使用 delete 和随后的 .push() 会影响性能/内存消耗吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34428162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com