简体   繁体   English

IE8中的jQuery和DOM操作性能问题

[英]jQuery and DOM Manipulation Performance Issue in IE8

I've developed a module at my work in JQuery, it is basically a table with the following functionality 我在JQuery的工作中开发了一个模块,它基本上是一个具有以下功能的表

  • Cell level Edit 单元格级别编辑
  • Row level edit 行级别编辑
  • Drop n drop rows to change position 删除n行以更改位置
  • Show/hide columns 显示/隐藏列
  • Column resize 列调整大小

every thing works fine on latest browsers like, FF9.0, IE9 and Chrome, but in older browsers like IE8 and FF3.6 as the number of rows in the table increases the performance of the page reduces significantly . 在FF9.0,IE9和Chrome等最新浏览器上,每件事情都可以正常工作, 但在IE8和FF3.6等旧版浏览器中,随着表格中行数的增加,页面性能显着降低 I've tried many optimization from jQuery and DOM manipulation but still no effect on the performance. 我已尝试过jQuery和DOM操作的许多优化,但仍然没有影响性能。 Any idea if I'm missing something or some tip to make the performance better ie to an acceptable level. 任何想法,如果我错过了什么或一些提示,使性能更好,即达到可接受的水平。

I haven't use any plugin, everything is my custom implementation. 我没有使用任何插件,一切都是我的自定义实现。 The javascript file is quite huge and I'm looking for some general good practices and tips. javascript文件非常庞大,我正在寻找一些一般的良好做法和技巧。

There are two major ways to improve performance with large html pages - reduce the number of page reflows and reduce the number of handlers. 有两种主要方法可以提高大型html页面的性能 - 减少页面重排的次数并减少处理程序的数量。

1. Reduce the number of page reflows 1.减少页面重排的次数

Every time you make a change to the DOM, it needs to redraw itself. 每次对DOM进行更改时,都需要重绘自己。 This is a reflow. 这是一个回流。 Keep these to a minimum by creating a string or DOM fragment with all your DOM manipulations, then inserting that into your page. 通过使用所有DOM操作创建字符串或DOM片段,然后将其插入到页面中,将这些保持在最低限度。 This will trigger only one reflow. 这将仅触发一次回流。 For example, if you're adding a table, create the whole table with text in it as should be, then insert that in a single operation. 例如,如果要添加表,请按原样创建包含文本的整个表,然后将其插入到单个操作中。

JQuery allows you to create a DOM fragment like this: JQuery允许您创建这样的DOM片段:

var table = $('<table></table');

You can manipulate your fragment in the standard ways: 您可以通过标准方式操作片段:

var line = $('<tr><td>Some Data</td></tr>');
line.css('color','red');
table.append(line);

Then when the fragment is complete, add it to the DOM in a single step: 然后当片段完成时,只需一步即可将其添加到DOM:

$('body').append(table);

you will trigger only one reflow and the process will be orders of magnitude faster. 您将只触发一次回流,并且该过程将快几个数量级。

2. Reduce the number of handlers 2.减少处理程序的数量

If you have a lot of controls on each row, that's a lot of handlers. 如果每行都有很多控件,那就是很多处理程序。 Instead create only one handler and attach it to the document root, then when it gets called inspect the target attribute to discover what to do. 而是只创建一个处理程序并将其附加到文档根目录,然后在调用它时检查目标属性以发现要执行的操作。 In JQuery you can use the new "on" handlers to do this, or else use the old "live" style handlers. 在JQuery中,您可以使用新的“on”处理程序来执行此操作,或者使用旧的“实时”样式处理程序。

for example: 例如:

$('table td').on('click', function() {
  //do work here
});

Only one handler will be created which will handle all of the td click events. 将只创建一个处理所有td点击事件的处理程序。

Do both of these and you should see dramatically improved performance. 做这两件事你应该看到显着改善的性能。

The bad news is, there's not really much you can do as it mostly depends on the javascript engine used in the browser. 坏消息是,你可以做的并不多,因为它主要取决于浏览器中使用的javascript引擎。

If it's an option for you try Google's Chromeframe for IE8, but on a public website that's most likely not a very nice solution. 如果您可以选择使用Google的Chromeframe for IE8,但在公共网站上这很可能不是一个非常好的解决方案。 But it can be in a corporate environment when users can easily update software. 但是,当用户可以轻松更新软件时,它可以在企业环境中使用。

You could also try to render the table on-the-fly: 您还可以尝试即时渲染表格:

Ao you got your table and you got an array with information in javascript (or pullable via ajax), plus you got informations about where you are in the table (row) and how long the table is (maxrows). 你得到了你的桌子,你得到了一个包含javascript信息的数组(或通过ajax进行拉取),另外还有关于你在表格(行)中的位置以及表格的长度(最大值)的信息。

Then create a table that's only as big as the viewport, maybe a little bit bigger. 然后创建一个只有视口大的表,可能更大一点。 Everything above and below the viewport is handled by a big <div> or anything that is streched to the height of remaining rows or rows before the topmost in-viewport row. 视口上方和下方的所有内容都由一个大的<div>或在最顶层的视口内行之前拉伸到剩余行或行的高度的任何内容处理。

That way only a very limited number of dom-nodes is present at any time. 这样,任何时候都只存在非常有限数量的dom节点。 It could possibly improve performance. 它可能会提高性能。

When the user scrolls remove table cells that are no longer in viewport (and add their height to the respective whitespace block of that side) and add table cells that freshly came into viewport (removing the height from the whitespace on that side). 当用户滚动删除不再在视口中的表格单元格(并将其高度添加到该侧面的相应空白块)并添加刚刚进入视口的表格单元格(从该侧的空白处移除高度)。

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

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