并在jQuery的DOM元素的手工垃圾收集提高浏览器性能呢? [英] does manual garbage collection of DOM elements in jquery improve browser performance at all?

查看:88
本文介绍了并在jQuery的DOM元素的手工垃圾收集提高浏览器性能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对性能的范围,它任何意义,除去那些不再需要的元素?或做浏览器的DOM元素,未进一步code引用执行自动垃圾收集?

  $('some_element')。淡出(1000,功能(EL){
   $(EL)一个.remove(); //< - 这是否有道理呢?
});


解决方案

这code:

  $('some_element')删除()。

告诉您正在使用元素来完成,不再需要在DOM的浏览器。如果你没有在你的JavaScript来该元素的任何其他引用,垃圾收集器会然后释放内存,它使用。

如果你不删除它,然后将DOM元素停留在你的网页,只要将显示该网页。它永远不会被垃圾收集,因为浏览器无法知道您是否打算为它停留在页面与否的方法。

这是一个很好的做法,手动删除不再需要的DOM元素。

但是,在99%的情况,也不会任何的方式,因为用由一个DOM元素的存储器相比较于由网页所使用的总存储器非同小可英寸当用户进入到另一个网页反正在网页中一切都将被释放。

主要的时间,它确实让自由东西显著差异一样,这是当你一遍又一遍地做同样的操作再次(在一个大循环,一个计时器,等等)。在这种情况下,你不希望对象堆积和消耗增加内存量为网页时使用。

with scope on performance, does it make any sense to remove elements that are not longer needed? or do browsers perform auto garbage collection on dom elements, that are not further referenced in code?

$('some_element').fadeOut(1000, function(el){
   $(el).remove(); // <-- does this make sense at all?
});

解决方案

This code:

$('some_element').remove();

tells the browser that you are done with that element and no longer need it in the DOM. If you don't have any other references in your javascript to that element, the garbage collector will then free the memory that it uses.

If you do not remove it, then the DOM element stays in your web page for as long as that web page is displayed. It will never be garbage collected because the browser has no way of knowing whether you intend for it to stay in the page or not.

It is a good practice to manually remove DOM elements that are no longer needed.

But, in 99% of the cases, it will not matter in any way because the memory used by one DOM element is trivial compared to the overall memory used by a web page. Everything in the web page will be freed when the user goes to another web page anyway.

The main time that it does make a significant difference to free something like this is when you are doing the same operation over and over again (in a big loop, on a timer, etc...). In that case, you do not want objects to pile up and consume increasing amounts of memory as the page is used.

这篇关于并在jQuery的DOM元素的手工垃圾收集提高浏览器性能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆