简体   繁体   English

通过Javascript向DOM中添加大量HTML时,IE太慢

[英]IE too slow when adding a lot of HTML to the DOM via Javascript

I have a page where we are using a Kendo UI template to generate a bunch of HTML to insert into the DOM. 我有一个页面,我们在其中使用Kendo UI模板生成一堆HTML,以插入到DOM中。 We are talking 2+ million bytes. 我们正在谈论2+百万字节。 All the other browsers will display this in about 2 to 3 seconds or so. 所有其他浏览器将在大约2到3秒左右的时间内显示此内容。 I'm already avoiding jQuery and setting the innerHTML via the document.getByElementId call. 我已经在避免使用jQuery,并通过document.getByElementId调用设置了innerHTML。 It appears that the slowest part is regexp.test calls that IE makes. 看来,最慢的部分是IE进行的regexp.test调用。 Is IE vaidating that all the start/end tags match? IE是否证明所有开始/结束标记都匹配? If so, is there a way to tell IE to skip this check to speed things up (ie "Trust me")? 如果是这样,是否有办法告诉IE跳过此检查以加快处理速度(即“信任我”)? I've already reduced the HTML needed to as small as possible already, so that is really no longer an option. 我已经将所需的HTML减小到尽可能的小,因此实际上不再是一种选择。 Also, the data I'm showing isn't even the largest dataset yet, so I know it'll get a lot bigger. 另外,我显示的数据甚至还不是最大的数据集,所以我知道它会变得更大。 Does anyone have any ideas? 有人有什么想法吗?

Update: 更新:

This is the current code I'm using: 这是我正在使用的当前代码:

 var resultHtml = kendo.template(templateHtml, { useWithBlock: false })(currentPage.ViewModel);
 resultHtml = resultHtml.replace(new RegExp("\>[\n\t ]+\<", "g"), "><"); // Get rid of whitespace
 document.getElementById("tblData").innerHTML = resultHtml;

In IE, this ends up taking about 10 seconds to show with 2.5 million characters being generated. 在IE中,这大约需要10秒钟才能显示出250万个字符。 In Chrome/FireFox/Opera, this takes about 2 to 3 seconds. 在Chrome / FireFox / Opera中,这大约需要2到3秒钟。

Implement something like "double buffering". 实现类似“双重缓冲”的功能。

Create a node that is not part of the DOM. 创建一个不属于DOM的节点。

var offlineNode = $('<div />');

Render your contents to the offline node. 将您的内容呈现到脱机节点。

offline.html(kendoTemplate(billionData));    

Once you finish, move the entire node to the DOM. 完成后,将整个节点移至DOM。

offlineNode.appendTo($('#a-place-in-dom'));

IE has problems guessing when to re-render the page, so it is sensible to DOM manipulation. IE在猜测何时重新呈现页面时遇到问题,因此对DOM操作很明智。 Every time you make massive DOM changes, IE will be slow. 每次您进行大量DOM更改时,IE都会变慢。

The problem does not appear to be the HTML being added to the DOM now. 问题似乎不是HTML现在添加到DOM中。 It has to do with all the objects I have to 2-way bind with Kendo MVVM. 它与我必须用Kendo MVVM 2路绑定的所有对象有关。 Sorry about that! 对于那个很抱歉!

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

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