什么时候应该在 JavaScript 中使用 delete 和将元素设置为 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1947995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When should I use delete vs setting elements to null in JavaScript?
提问by jeffreyveon
Possible Duplicate:
Deleting Objects in JavaScript
可能的重复:
在 JavaScript 中删除对象
I have a JS object having a large number of properties. If I want to force the browser to garbage collect this object, do I need to set each of these properties as null or do I need to use the delete operator? What's the difference between the two?
我有一个具有大量属性的 JS 对象。如果我想强制浏览器对这个对象进行垃圾回收,我需要将这些属性中的每一个都设置为 null 还是需要使用 delete 运算符?两者有什么区别?
回答by Annabelle
There is no way to force garbage collection in JavaScript, and you don't really need to. x.y = null;and delete x.y;both eliminate x's reference to the former value of y. The value will be garbage collected when necessary.
在 JavaScript 中没有强制垃圾回收的方法,你也不需要这样做。x.y = null;并且delete x.y;都消除了x对 的前一个值的引用y。必要时,该值将被垃圾收集。
If you null out a property, it is still considered 'set' on the object and will be enumerated.The only time I can think of where you would prefer deleteis if you were going to enumerate over the properties of x.
如果您将一个属性设为 null,它仍被视为对象上的“设置”并将被枚举。我能想到的唯一一次你更喜欢的地方delete是,如果你要枚举x.
Consider the following:
考虑以下:
var foo = { 'a': 1, 'b': 2, 'c': 3 };
console.log('Deleted a.');
delete foo.a
for (var key in foo)
console.log(key + ': ' + foo[key]);
console.log('Nulled out b.');
foo['b'] = null;
for (var key in foo)
console.log(key + ': ' + foo[key]);
This code will produce the following output:
此代码将产生以下输出:
Deleted a.
b: 2
c: 3
Nulled out b.
b: null
c: 3
回答by Matt DiMeo
Javascript objects properties are typically implemented with a hashtable. Setting to null leaves the key in the hashtable pointing to a null value, while delete eliminates both the key and the value.
Javascript 对象属性通常使用哈希表实现。设置为 null 会使哈希表中的键指向空值,而 delete 会同时消除键和值。
The main observable difference between the two is that if you iterate over the keys with a for..in loop, deleting keys results in fewer entries seen by the iteration.
两者之间可观察到的主要区别在于,如果您使用 for..in 循环遍历键,删除键会导致迭代看到的条目更少。
I would suggest preferring deletion in general, unless you are going to be repeatedly setting and clearing the same keys, which would argue for leaving the hash table structure in place. However, any performance difference between the two techniques is going to be immeasurably small in any typical case.
我建议一般情况下更喜欢删除,除非您要重复设置和清除相同的键,否则会争论保留哈希表结构。然而,在任何典型情况下,这两种技术之间的任何性能差异都将是不可估量的。
-m@
-m@
回答by Gabriel McAdams
There is no way to force garbage collection at a specific time in JavaScript. Each JavaScript engine has its own way of handling that.
JavaScript 无法在特定时间强制进行垃圾回收。每个 JavaScript 引擎都有自己的处理方式。
What you can do, though, is make sure your objects aren't being referenced anywhere. Setting them to null or deleting them are actually doing the same thing (delete never deletes the object - it only removes a reference to it). When garbage collection does run, it checks to see if there are any references to a given object - then, if not, it removes it from memory. This is where memory leaks occur (a JavaScript object is referencing a DOM object, and vice-versa).
但是,您可以做的是确保您的对象没有在任何地方被引用。将它们设置为 null 或删除它们实际上是在做同样的事情(删除永远不会删除对象 - 它只会删除对它的引用)。当垃圾收集确实运行时,它会检查是否存在对给定对象的任何引用 - 如果没有,它将从内存中删除它。这是发生内存泄漏的地方(JavaScript 对象正在引用 DOM 对象,反之亦然)。
Make sure you remove all references and you'll be fine.
确保删除所有引用,你会没事的。

