简体   繁体   English

为什么使用innerHTML比JQuery的append()函数更慢地将大量元素附加到DOM?

[英]Why is using innerHTML to append a large number of elements to the DOM slower than JQuery's append() function?

I have a vanilla JavaScript function to test appending large numbers of elements to the DOM: 我有一个vanilla JavaScript函数来测试向DOM添加大量元素:

var start = new Date().getTime();
var blah;
var div = document.getElementById("me");
for (i = 0; i < 5000; ++i) {
    div.innerHTML += "<div>" + i + "</div>";//Simply add to div.
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);

Results: 结果:

         Chrome           IE10
         ------           -----  
Vanilla      39             130  (seconds)

JQuery: JQuery的:

for (i = 0; i < 5000; ++i) {
    $("#me").append("<div>" + i + "</div>");//Now using append instead.
}

Results 结果

         Chrome           IE10
         ------           -----  
Vanilla   39000         130,000  (milliseconds) 
JQuery      260            1300  (milliseconds) 

NB : It didn't seem to have any impact on performance if I used the $("#me") selector or passed in $(div) 注意 :如果我使用$("#me")选择器或传入$(div)它似乎对性能没有任何影响

Vanilla with AppendChild : 香草与AppendChild

for (i = 0; i < 5000; ++i) {
    var el = document.createElement("div");//Now create an element and append it.
    el.innerHTML = i;
    div.appendChild(el);
}
             Chrome           IE10
             ------           -----  
Vanilla       39000         130,000  (ms)   
JQuery          260            1300  (ms) 
AppendChild      30             240  (ms)

To my huge surprise this was by far the fastest, most performant. 令我惊讶的是,这是迄今为止最快,最高效的。 On Chrome it takes a whopping 30ms or so, and on IE it takes around 240ms. 在Chrome上它需要大约30ms左右,在IE上需要大约240ms。

You can play with all the variations here: Fiddle 你可以在这里玩所有的变化: 小提琴

I know there could be many other variations to test, but what is jQuery doing behind the scenes to make it's .append() so much faster than native JS innerHTML += and why is creating a new element and appending it even faster? 我知道可能还有许多其他版本要测试,但是jQuery在幕后做什么才能使它的.append()比原生JS innerHTML +=快得多,为什么创建一个新元素并将其追加得更快?

If you do things right , you can pretty much double your "best" result. 如果你做得对 ,你可以将你的“最佳”结果翻倍。

Native DOM methods are always faster than their jQuery alternatives. 原生DOM方法总是比它们的jQuery替代方法更快。 However, .innerHTML is not ideal. 但是, .innerHTML并不理想。

When you use .innerHTML += ... , here's what happens: 当你使用.innerHTML += ... ,会发生以下情况:

  1. Build an HTML representation of the entire DOM that current exists 构建当前存在的整个DOM的HTML表示
  2. Append your new string to it 将新字符串附加到其中
  3. Parse the result and create a whole new DOM tree from it 解析结果并从中创建一个全新的DOM树
  4. Put the new stuff in place of the old stuff 把新东西放在旧东西的位置

The native methods are significantly less work ;) 本机方法的工作量明显减少;)

It should also be noted that innerHTML += ... completely nukes the DOM , meaning any references to the old elements are lost and in particular event handlers are not kept (unless you used inline event handlers, which you shouldn't be) 还应该注意, innerHTML += ... 完全核对DOM ,这意味着对旧元素的任何引用都会丢失,特别是事件处理程序不会被保留(除非你使用了内联事件处理程序,你不应该这样做)

Behind the scenes, jQuery is using document fragments , which perform much better than straight manipulation of the document. 在幕后, jQuery使用的是文档片段 ,它比直接操作文档要好得多。 John Resig discussed document fragments' superior performance in 2008 , which should give you a solid explanation about what jQuery is doing and why. John Resig在2008年讨论了文档片段的优异性能 ,它可以为您提供有关jQuery正在做什么以及为什么做的可靠解释。

It seems to me that it would be much more efficient if you calculated everything you wished to append beforehand, then append that - minimize the DOM manipulation required. 在我看来,如果你预先计算了你想要追加的所有内容,那么它会更有效率,然后追加它 - 最小化所需的DOM操作。 eg: 例如:

var toAppend = "";
for (i = 0; i < 5000; ++i) {
   toAppend += "<div>" + i + "</div>";
}
div.append(toAppend)

If you're wanting them to be nested, you could make it recursive, or come up with some other solution. 如果您希望它们嵌套,您可以使其递归,或者提出其他解决方案。 either way, I believe string manipulation will always be faster than DOM manipulation 无论哪种方式,我相信字符串操作总是比DOM操作更快

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

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