简体   繁体   English

有太多dom节点的Javascript性能问题?

[英]Javascript performance problems with too many dom nodes?

I'm currently debugging a ajax chat that just endlessly fills the page with DOM-elements. 我正在调试一个ajax聊天,它无休止地用DOM元素填充页面。 If you have a chat going for like 3 hours you will end up with god nows how many thousands of DOM-nodes. 如果你有3个小时的聊天时间,你最终会知道有多少个DOM节点。

What are the problems related to extreme DOM Usage? 极端 DOM使用相关的问题是什么?

Is it possible that the UI becomes totally unresponsive (especially in Internet Explorer)? 用户界面是否可能完全没有响应(特别是在Internet Explorer中)?

(And related to this question is off course the solution, If there are any other solutions other than manual garbage collection and removal of dom nodes.) (与此问题相关的当然是解决方案,如果除了手动垃圾收集和删除dom节点之外还有其他任何解决方案。)

Most modern browser should be able to deal pretty well with huge DOM trees. 大多数现代浏览器应该能够很好地处理巨大的DOM树。 And "most" usually doesn't include IE. 而“大多数”通常不包括IE。

So yes, your browser can become unresponsive (because it needs too much RAM -> swapping) or because it's renderer is just overwhelmed. 所以,是的,你的浏览器可能会变得没有响应(因为它需要太多RAM - >交换)或因为它的渲染器只是不堪重负。

The standard solution is to drop elements, say after the page has 10'000 lines worth of chat. 标准解决方案是删除元素,比如在页面有10,000行的聊天之后。 Even 100'000 lines shouldn't be a big problem. 即使是100'000线也不应该是一个大问题。 But I'd start to feel uneasy for numbers much larger than that (say millions of lines). 但是我开始对比这大得多的数字感到不安(比如数百万行)。

[EDIT] Another problem is memory leaks. [编辑]另一个问题是内存泄漏。 Even though JS uses garbage collection, if you make a mistake in your code and keep references to deleted DOM elements in global variables (or objects references from a global variable), you can run out of memory even though the page itself contains only a few thousand elements. 即使JS使用垃圾收集,如果您在代码中出错并在全局变量中保留对已删除DOM元素的引用(或者从全局变量引用对象),即使页面本身只包含少量内容,也会耗尽内存千元素。

Just having lots of DOM nodes shouldn't be much of an issue (unless the client is short on RAM); 只是拥有大量的DOM节点应该不是什么大问题(除非客户端缺少RAM); however, manipulating lots of DOM nodes will be pretty slow. 但是, 操纵大量DOM节点会非常慢。 For example, looping through a group of elements and changing the background color of each is fine if you're doing this to 100 elements, but may take a while if you're doing it on 100,000. 例如,循环浏览一组元素并更改每个元素的背景颜色,如果您对100个元素进行此操作就可以了,但如果您在100,000个元素上执行此操作可能需要一段时间。 Also, some old browsers have problems when working with a huge DOM tree--for example, scrolling through a table with hundreds of thousands of rows may be unacceptably slow. 此外,一些旧的浏览器在使用巨大的DOM树时会遇到问题 - 例如,滚动数十万行的表可能会慢得令人无法接受。

A good solution to this is to buffer the view. 一个很好的解决方案是缓冲视图。 Basically, you only show the elements that are visible on the screen at any given moment, and when the user scrolls, you remove the elements that get hidden, and show the ones that get revealed. 基本上,您只显示在任何给定时刻在屏幕上可见的元素,当用户滚动时,您将删除隐藏的元素,并显示显示的元素。 This way, the number of DOM nodes in the tree is relatively constant, but you don't really lose anything. 这样,树中DOM节点的数量相对恒定,但实际上并没有丢失任何东西。

Another similar solution to this is to implement a cap on the number of messages that are shown at any given time. 另一个类似的解决方案是对任何给定时间显示的消息数量实施上限。 This way, any messages past, say, 100 get removed, and to see them you need to click a button or link that shows more. 通过这种方式,过去的所有邮件(例如100)都会被删除,要查看它们,您需要点击显示更多内容的按钮或链接。 This is sort of what Facebook does with their profiles, if you need a reference. 如果您需要参考,这就是Facebook对其个人资料的处理方式。

Problems with extreme DOM usage can boil down to performance. 极端 DOM使用的问题可以归结为性能。 DOM scripting is very expensive, so constantly accessing and manipulating the DOM can result in a poor performance (and user experience), particularly when the number of elements becomes very large. DOM脚本非常昂贵,因此不断访问和操作DOM会导致性能(和用户体验)不佳,特别是当元素数量变得非常大时。

Consider HTML collections such as document.getElementsByTagName('div') , for example. 例如,考虑HTML集合,例如document.getElementsByTagName('div') This is a query against the document and it will be reexecuted every time up-to-date information is required, such as the collection's length. 这是对文档的查询,每次需要最新信息时都会重新执行,例如集合的长度。 This could lead to inefficiencies. 这可能导致效率低下。 The worst cases will occur when accessing and manipulating collections inside loops. 访问和操作循环内的集合时会出现最糟糕的情况。

There are many considerations and examples, but like anything it depends on the application. 有许多考虑因素和例子,但它们取决于应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM