通过innerHTML进行DOM操作的性能测试

发布于 2024-09-07 04:50:59 字数 1141 浏览 0 评论 0原文

根据 stackoverflow 的精神(感谢 @Bill the Lizard 澄清了这一点),我想介绍一下自己对所有前端 Web 开发人员都熟知的问题的小型研究。

如何在使用innerHTML或DOM操作进行DHTML编程时获得更好的jscript性能?

为了进行测试,我创建了带有“开始测试”按钮的单个 HTML 页面,该页面运行了 5000 次函数对,该函数是根据 innerHTML 意识形态或 DOM 编写的。 (可以在: http://codepad.org/HyiKRsNk

  • 避免显示比较 CPU 结果以百分比表示,一个函数比另一个函数快的程度。因此,如果您看到(17%),则意味着 DOM 更快的innerHTML,如果(可能是后者)您看到(-5%),则意味着innerHTML 比 DOM 更快。
  • 为了避免内存爆炸的影响,对于每个调用,都会在测试后立即创建和删除新的“测试区域”(不遇到创建/删除的时间)。
  • 测试在 display:none div 下执行,以避免滚动时翻转和性能下降

我准备了 3 个测试(@tomdemuyt 提出的最后一个)

  1. 为其创建新的 DIV 和子文本

  2. 删除一些DIV(为了避免创建差异的影响,两个测试都使用相同的方式创建测试div,但删除不同)

  3. 添加 onclick 事件处理程序

  4. 深入测试树的创建,测试创建 ;
    文本构造。 DOM 测试使用 createElement/appendChild 对的顺序调用
  5. 与 (4) 类似,但创建了 2 行 。 DOM 使用特殊的辅助函数来简化:

    treeHelper("表", 
        树助手(“tbody”, 
            树助手(“tr”, 
    

针对不同浏览器的测试结果如下。

所以亲爱的同事,请提供:当innerHTML更快时显示一些测试,在未列出的浏览器上开始我的测试。

According to spirit of stackoverflow (thanks to @Bill the Lizard clarifying this) I would like to introduce small own research of the well known to all front-end web developers question.

How to get better performance of jscript while programming DHTML, using innerHTML or DOM operations?

For the testing I have created single HTML page with “start test” button that runs 5000 times pair of function, that is written either on innerHTML ideology, or by DOM. (It can be taken on: http://codepad.org/HyiKRsNk )

  • To avoid compare CPU result are displayed in percent, how one of function is faster then another. So if you see (17%) it is mean that DOM faster innerHTML, if (may be latter) you see (-5%) it is mean innerHTML faster than DOM.
  • To avoid effect of memory blow-up, for each call new "test area" is created and deleted immediately after test (time for creation/deletion is not encountered).
  • Test are performed under display:none div to avoid flipping and performance degradation on scrolling

I have prepared 3 test (the last one proposed by @tomdemuyt)

  1. create new DIV and child text for it

  2. remove some DIV (to avoid influence of creation difference, both tests uses same way to create test div, but different for removing)

  3. adding onclick event handler

  4. Test creation of tree in depth, test create <table><tbody><tr><td><span>text construction. DOM test uses sequential calls of pair createElement/appendChild
  5. Similar as (4) but 2 rows <tr> are created. DOM uses special helper function to simplify:

    treeHelper("table", 
        treeHelper("tbody", 
            treeHelper("tr", 
    

Test results are followed for different browsers.

So dear colleagues please provide: show some test when innerHTML is faster, start my test on non-listed-yet browsers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柒七 2024-09-14 04:50:59

根据对原始问题的评论,我在此处添加了一个答案,详细说明了为什么很难简洁地评估直接 .innerHTMLDOM 操作 的比较。

本质上有几个主要问题:

  1. 浏览器错误和问题。缺少实现
  2. 内容差异(在情况 A 中效果更好,在情况 B 中可能效果不佳)
  3. 时间问题(JavaScript 无法控制或洞察浏览器何时决定进行 GC(垃圾收集)或运行其他线程/活动

1. )特别是 IE(6,7 和较小程度的 8)有一堆浏览器错误,导致对所有代码场景进行全面测试。 (IE 有错误,无法在 selecttablethead上设置 .innerHTML >tbodytfoottr,溢出设置为 auto 的空元素,其中内容高度超过 1“行”,pre< /code> 元素,其中内容包含重要的空格等。)

2.) 添加的子节点的数量、类型(顺序)和深度将改变操作的性能。例如,通过添加表标签,然后添加 tbody,然后添加 tr,然后添加 td,然后添加内容来添加表,而不是单独构建该表并将其转储到页面中,这会改变性能。

3.) 浏览器控制何时执行 GC(这会在清理时使浏览器减慢一段时间)。与浏览器扩展或另一个选项卡中的 AJAX 调用类似,IE8 中的 Web 切片会轮询服务器以获取更新,这将影响当前页面的执行方式。

因此,如果您想对此进行全面测试,则需要测试 HTML 子结构的几种不同组合,我非常希望结果会有所不同。

我家里有一些针对此类事情的旧测试,我将挖掘这些测试并将其添加到此答案中以进行更多涉及的测试。

Based on the comments on the original question I'm adding an answer here that elaborates on why a straight up .innerHTML to DOM manipulation comparison is very hard to concisely evaluate.

Essentially there are a few major concerns:

  1. Browser bugs & missing implementations
  2. Content differences (what works better in case A, may not be better in case B)
  3. Timing issues (JavaScript has no control or insight as to when the browser decides to do GC (Garbage Collection) or run other threads/activities

1.) In particular IE(6,7 & to a lesser extent 8) has a bunch of browser bugs that cause full testing of all code scenarios. (IE has bugs and can't set the .innerHTML on a select, table, thead,tbody,tfoot,tr, empty elements with overflow set to auto where the content is more than 1 "row" in height, pre elements where the content contains important whitespace etc.)

2.) The quantity, type (order) and depth of child nodes added will alter the performance of the operation. E.g. adding a table by adding the table tag, then a tbody, then a tr, then a td, then content vs. building this up separately and dumping it into the page will alter the performance.

3.) The browser controls when it does a GC (which will slow the browser for a moment while it does cleanup). Likewise a browser extension, or AJAX call in another tab, web slice in IE8 polls a server for updates it will affect how your current page performs.

Thus if you want to fully test this, you'd need to test several different combinations of HTML sub-structure and I would highly expect that the results will vary.

I have some old tests for this kind of thing at home that I'll dig up and add to this answer for more involved testing.

水染的天色ゝ 2024-09-14 04:50:59

Opera (10.54):

Test                                Result 
1.insert new element and text         30.00%
2.removeChild vs innerHTML=''         66.67%
3.set event handler                   245.45%
4.test in depth of tree linear        -62.16%
5.test in depth of tree (using helpr) -67.10%

Chrome (5.0.375.70):

Test                                 Result
1.insert new element and text          318.18%
2.removeChild vs innerHTML=''          53.85%
3.set event handler                    150.00%
4.test in depth of tree linear         41.67%
5.test in depth of tree (using helpr) -33.03%

FireFox(3.6.4):

Test                                  Result
1.insert new element and text           76.17%
2.removeChild vs innerHTML=''           50.56%
3.set event handler                     1097.44%
4.test in depth of tree linear          32.94%
5.test in depth of tree (using helpr)   9.66%

IE(8):

Test                                  Result 
1.insert new element and text           46.92%
2.removeChild vs innerHTML=''           26.34%
3.set event handler                     936.17%
4.test in depth of tree linear          -35.42%
5.test in depth of tree (using helpr)   -67.26%

编辑(1):
为每个浏览器添加了 2 行,用于深度构建树的测试(线性树深度测试、树深度测试(使用 helpr))。人们发现大多数浏览器不喜欢堆栈分配值,因为线性的appendChild,appendChild ...比在treeHelper('table',treeHelper('tbody',...)上做同样的事情要快得多。

Opera (10.54):

Test                                Result 
1.insert new element and text         30.00%
2.removeChild vs innerHTML=''         66.67%
3.set event handler                   245.45%
4.test in depth of tree linear        -62.16%
5.test in depth of tree (using helpr) -67.10%

Chrome (5.0.375.70):

Test                                 Result
1.insert new element and text          318.18%
2.removeChild vs innerHTML=''          53.85%
3.set event handler                    150.00%
4.test in depth of tree linear         41.67%
5.test in depth of tree (using helpr) -33.03%

FireFox(3.6.4):

Test                                  Result
1.insert new element and text           76.17%
2.removeChild vs innerHTML=''           50.56%
3.set event handler                     1097.44%
4.test in depth of tree linear          32.94%
5.test in depth of tree (using helpr)   9.66%

IE(8):

Test                                  Result 
1.insert new element and text           46.92%
2.removeChild vs innerHTML=''           26.34%
3.set event handler                     936.17%
4.test in depth of tree linear          -35.42%
5.test in depth of tree (using helpr)   -67.26%

EDITED(1):
Added for each browser 2 lines with test of building tree in depth (test in depth of tree linear, test in depth of tree (using helpr)). It was discovered that most browsers don't like stack allocation values, since linear appendChild, appendChild... much faster then doing the same over treeHelper('table', treeHelper('tbody', ....

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