简体   繁体   English

在 IE/Edge 中,jQuery html 元素创建速度非常慢? 有什么我可以做的吗?

[英]jQuery html element creation extremely slow in IE/Edge? Is there anything I can do?

Update: jsfiddle/v4更新: jsfiddle/v4

Silly typo in the demo had both executions running at 1000 rather than 10,000.演示中愚蠢的错别字让两次执行都以 1000 而不是 10,000 运行。

At 10,000 Edge stalls out, which is what I'm seeing a lot of in my website.在 10,000 Edge 停顿时,这是我在我的网站上看到的很多。 At 1,000, it just takes a few seconds.在 1,000 时,只需几秒钟。

Still, after more than five solid minutes of waiting on Edge, I closed the process where chrome did it in less than 5 seconds.尽管如此,在 Edge 上等待了超过 5 分钟后,我关闭了 chrome 在不到 5 秒内完成的过程。

A sample run across the browsers mentioned has these results.在提到的浏览器上运行的示例具有这些结果。 The first number is the test that renders to the screen, the second number populates a jQuery-created element.第一个数字是呈现到屏幕的测试,第二个数字填充 jQuery 创建的元素。

Browser:    To Screen   To jQuery-created element, not injected into DOM.
IE/Edge:    Stalled out.
Chrome:     4519        3757
Firefox:    4032        4092
Opera:      16925       16450

I experienced some crashes in these tests, because I don't think any browser likes tags nested 10,000 deep, but that's not at all what I'm actually doing anyway.我在这些测试中遇到了一些崩溃,因为我认为没有任何浏览器喜欢嵌套 10,000 层深的标签,但这根本不是我实际在做的事情。 I experienced no crashes with the test that writes to a jquery-created element, other than MS's browsers.除了 MS 的浏览器之外,我没有遇到写入 jquery 创建的元素的测试崩溃。


I'm having a problem where IE 11 & Edge both take an extremely long time when creating a large number of elements.我遇到了一个问题,即 IE 11 和 Edge 在创建大量元素时都需要很长时间。 Chrome/Firefox/Opera all execute the script unbelievably fast. Chrome/Firefox/Opera 都以令人难以置信的速度执行脚本。 Even my LG Optimus's Chrome browser doesn't struggle.甚至我的 LG Optimus 的 Chrome 浏览器也没有问题。 But Edge/IE can take several minutes.但 Edge/IE 可能需要几分钟时间。

For whatever it's worth, I'm on windows 10.不管它值多少钱,我都在 Windows 10 上。

I've created a very contrived example that nests the same element within itself 10,000 times and counts the length immediately after it's done.我创建了一个非常人为的示例,该示例将同一元素嵌套 10,000 次,并在完成后立即计算长度。

It isn't that it's taking the browser time to display the elements, as shown with the second button that takes an equal amount of time even though the elements aren't injected into the DOM.并不是说浏览器需要花费时间来显示元素,如第二个按钮所示,即使元素没有注入到 DOM 中也需要相同的时间。

jsfiddle/v3 jsfiddle/v3

Is there anything I can do aside from the classic javascript route of "<span>" + variable + "</span>" ?除了"<span>" + variable + "</span>"的经典 javascript 路线之外,我还能做些什么吗? It's just much cleaner to be able to to use the jQuery method.能够使用 jQuery 方法要干净得多。

I have googled for this, but I haven't hit on the right combination of words if there are other posts on the topic.我已经用谷歌搜索过这个,但如果有关于该主题的其他帖子,我还没有找到正确的单词组合。

(jsfiddle/v4 code) (jsfiddle/v4 代码)

$(document).ready(function () {
   $("#clk").click(function () {
       StartTime = new Date().getTime();
       var htm = $("<span>")
       for (var i=0; i<10000; i++) {
           htm = $("<span>").append(htm);
       }
       $("#recip").html(htm);
       EndTime = new Date().getTime();
       TotalTime = EndTime - StartTime;
       alert(TotalTime + "ms")
   });
   
   var $recip = $("<div>");
   $("#clk2").click(function () {
       StartTime = new Date().getTime();
       var htm = $("<span>")
       for (var i=0; i<10000; i++) {
           htm = $("<span>").append(htm);
       }
       $recip.html(htm);
       EndTime = new Date().getTime();
       TotalTime = EndTime - StartTime;
       alert(TotalTime + "ms")
   });
});

正如这里所解释的一个选项(如果它适合您的需要)是将整个 html 创建为文本,然后在最后将其作为一个整体附加。

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

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