为什么jQuery的.data()函数更能防止内存泄漏?
关于 jQuery 实用函数 jQuery.data() 在线文档说:
“jQuery.data() 方法允许我们 将任何类型的数据附加到 DOM 元素以安全的方式 循环引用,因此来自 内存泄漏。 ”
为什么使用:
document.body.foo = 52;
可能会导致内存泄漏 - 或者在什么条件下 - 所以我应该使用
jQuery.data(document.body, 'foo', 52);
Should I ALWAYS Preferred .data() 而不是在任何情况下使用 Expandos?
(如果您能提供一个示例来比较,我将不胜感激差异)
谢谢,
burak ozdogan
Regarding to jQuery utility function jQuery.data() the online documentation says:
"The jQuery.data() method allows us to
attach data of any type to DOM
elements in a way that is safe from
circular references and therefore from
memory leaks. "
Why to use:
document.body.foo = 52;
can result a memory leak -or in what conditions- so that I should use
jQuery.data(document.body, 'foo', 52);
Should I ALWAYS prefer .data() instead of using expandos in any case?
(I would appreciate if you can provide an example to compare the differences)
Thanks,
burak ozdogan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它更好,正是因为它在您给出的引用中说:“安全免受循环引用。”
假设您有变量
nodeOne
和nodeTwo
,哪些参考节点。假设您将其放入一个函数中(您不存储其引用):
函数运行后,会出现循环引用:nodeOne 具有对nodeTwo 的引用,反之亦然。
通过使用 jQuery.data,循环引用不会阻止这两个变量被垃圾收集。
但是,如果您要执行相同的操作但不使用 jQuery.data,则
nodeOne
和nodeTwo
变量将不会被垃圾回收,即使如果不再需要变量。--
除非您正在进行大量数据设置并且需要任何额外的性能下降(并且您可以通过使用分析来判断)并且确定您不会创建循环引用(或者在至少有一个重要的数字),那么是的,您也可以只使用 jQuery.data。
It's better precisely because of something it says in the quote you gave: "safe from circular references."
Say you have variables
nodeOne
andnodeTwo
, which reference nodes.Say you then put this in a function (whose reference you don't store):
After the function runs, there's a circular reference: nodeOne has a ref to nodeTwo, and vice versa.
By using jQuery.data, that circular reference won't prevent those two variables from being garbage collected.
However, if you were to do the same thing but without using jQuery.data, the
nodeOne
andnodeTwo
variables would not be garbage collected, even if the variables are no longer needed.--
Unless you're doing large amounts of data setting and need any extra drops of performance (and you could tell by using profiling) and are sure you won't create circular references (or at least a number that would matter), then yes, you may as well only use jQuery.data.
我很确定您不能使用
52
这样的原始值引入内存泄漏。当应用的值包含引用回元素的对象时,通常会发生扩展的内存泄漏。我建议阅读 http://msdn.microsoft.com 上的循环引用标题的内容/en-us/library/Bb250448。更好的是,阅读全部内容 :-)
话虽如此,我认为大多数人建议如果可能的话不要使用 Expandos(通常是可能的)。无论如何,使用 jQuery 的
data()
都是一个不错的选择。Just realized I didn't answer your question lol. jQuery's
data()
provides a method that goes something like the following:data()
方法可用的对象中,使用唯一 ID每当您调用
data()
来获取数据时,jQuery都会访问唯一ID的expando属性,并使用该ID从缓存对象中获取数据。由于 Expando 包含原始值并且没有附加到缓存对象,因此不会发生循环引用。I'm pretty sure you can't introduce a memory leak with a primitive value like
52
. Memory leaks with expandos usually occur when a value is applied that contains an object with a reference back to the element.I suggest reading the contents of the Circular Reference heading at http://msdn.microsoft.com/en-us/library/Bb250448. Even better, read it all :-)
That being said, I think most people recommend against using expandos if possible (and it usually is possible). Using jQuery's
data()
is a good alternative, in any case.Just realized I didn't answer your question lol. jQuery's
data()
provides a method that goes something like the following:data()
method, using the unique IDWhenever you call
data()
to fetch the data, jQuery accesses the expando property for the unique ID and uses that ID to fetch the data from the caching object. Because the expando contains a primitive value and has no attachment to the caching object, no circular references occur.关于该主题的一些精彩读物:
JavaScript 中的内存泄漏模式
Some great reading on the subject:
Memory leak patterns in JavaScript