为什么jQuery的.data()函数更能防止内存泄漏?

发布于 2024-08-30 17:15:09 字数 455 浏览 3 评论 0原文

关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2024-09-06 17:15:09

它更好,正是因为它在您给出的引用中说:“安全免受循环引用。”

假设您有变量 nodeOnenodeTwo,哪些参考节点。

假设您将其放入一个函数中(您不存储其引用):

jQuery.data(nodeOne, 'item', nodeTwo);
jQuery.data(nodetwo, 'item', nodeOne);

函数运行后,会出现循环引用:nodeOne 具有对nodeTwo 的引用,反之亦然。

通过使用 jQuery.data,循环引用不会阻止这两个变量被垃圾收集。

但是,如果您要执行相同的操作但使用 jQuery.data,则 nodeOnenodeTwo 变量将不会被垃圾回收,即使如果不再需要变量。

--

我应该总是更喜欢 .data() 吗
在任何情况下都使用 Expandos 吗?

除非您正在进行大量数据设置并且需要任何额外的性能下降(并且您可以通过使用分析来判断)并且确定您不会创建循环引用(或者在至少有一个重要的数字),那么是的,您也可以只使用 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 and nodeTwo, which reference nodes.

Say you then put this in a function (whose reference you don't store):

jQuery.data(nodeOne, 'item', nodeTwo);
jQuery.data(nodetwo, 'item', nodeOne);

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 and nodeTwo variables would not be garbage collected, even if the variables are no longer needed.

--

Should I ALWAYS prefer .data() instead
of using expandos in any case?

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.

ˉ厌 2024-09-06 17:15:09

我很确定您不能使用 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:

  1. jQuery 计算数据的唯一 ID 数据
  2. 存储在 data() 方法可用的对象中,使用唯一 ID
  3. Expando 属性将唯一 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:

  1. jQuery computes a unique ID for the data
  2. The data is stored in an object available to the data() method, using the unique ID
  3. An expando property is applied to the element with the unique ID as a primitive value

Whenever 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.

囍笑 2024-09-06 17:15:09

关于该主题的一些精彩读物:

JavaScript 中的内存泄漏模式

Some great reading on the subject:

Memory leak patterns in JavaScript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文