DOM内存和CPU管理
我正在创建一个逐步构建的单页面(如果启用了 Javascript)“博客”,它使用 AJAX 来请求用户导航到的新页面的 HTML。
当用户导航到新页面时,它们将被一个接一个地添加到一个小窗口中的 DOM 中,并带有“overflow:hidden;”:
<div id="foo" style="width:200px; height:100px;">
<div id="bar" style="width:999999px">
</div>
</div>
当 AJAX 调用返回成功时,一个 div 将被附加到 #bar 中。
当 #foo 宽度之外存在大量隐藏页面时,会对浏览器产生什么影响?
当用户离开它们时,我是否需要从 DOM 中删除这些 div? 然后,如果用户选择再次导航到它们,我将需要发出新的 AJAX 请求。 :(
谢谢威廉
I am creating a progressively built single-page (if Javascript is enabled) "blog" which uses AJAX to request HTML for new pages the user is navigating to.
When the user navigates to new pages they will be added one after another into the DOM in a small window with "overflow: hidden;":
<div id="foo" style="width:200px; height:100px;">
<div id="bar" style="width:999999px">
</div>
</div>
When an AJAX call returns success a div will be appended into #bar.
How will it affect the browser when there are a lot of hidden pages outside the #foo width?
Do I need to remove the divs from the DOM as the user navigates away from them? Then I will need to make a new AJAX request if the user chooses to navigate to them again. :(
Thanks
Willem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论人们说 GC 会为你做什么,无论是在 JavaScript、C# 还是 Java 中,都要小心并忘记自动管理的愚蠢承诺。 明确地清理干净,睡个好觉。
原因很简单:当你脱离最简单的场景(浏览器的 JavaScript 和 C#/java 都是这种情况)时,闭包就会泄漏,并且泄漏非常严重。
No matter what people say GC will do for you, whether in JavaScript or C# or Java, watch out and forget the silly promise of automatic management. Clean it up explicitly and sleep well.
Very simple reason: closures leak and leak pretty bad the moment you move out of most simplistic scenarios (the case both for brower's JavaScript as well as C#/java).
现代浏览器布局引擎通常足够智能,不会处理隐藏的元素,因此不会占用太多 CPU 资源。 然而,在某些浏览器中添加大量具有高度复杂对象图的节点可能会很昂贵,所以我会小心这一点。 另请注意,即使它们没有布局,它们仍然作为 DOM 的一部分存在,如果这些节点很大,内存使用可能会成为一个问题。
Modern browser layout engines are generally smart enough to not process elements that are hidden, so it won't take much CPU power. However, adding large numbers of nodes with highly complex object graphs can be expensive in some browsers, so I'd be careful with this. Also note that even if they're not laid out, they're still there as part of the DOM, and memory usage could conceivably become a concern if these nodes are large.