简体   繁体   English

jQuery性能

[英]jQuery performance

imagine you have to do DOM manipulation like a lot (in my case, it's kind of a dynamic list). 想象您必须做很多DOM操作(就我而言,它是一个动态列表)。

Look at this example: 看这个例子:

var $buffer = $('<ul/>', {
                'class': '.custom-example',
  'css':  {
   'position':  'absolute',
   'top':   '500px'
  }
           });

$.each(pages[pindex], function(i, v){
 $buffer.append(v);
});

$buffer.insertAfter($root);

"pages" is an array which holds LI elements as jQuery object. “页面”是一个数组,将LI元素保存为jQuery对象。

"$root" is an UL element “ $ root”是UL元素

What happens after this code is, both UL's are animated (scrolling) and finally, within the callback of animate this code is executed: 这段代码之后发生的事情是,两个UL都进行了动画处理(滚动),最后,在动画的回调中执行了以下代码:

$root.detach();
$root = $buffer;
$root.css('top', '0px');
$buffer = null;

This works very well, the only thing I'm pi**ed off is the performance. 效果很好,我唯一想得到的就是性能。 I do cache all DOM elements I'm laying a hand on. 我确实缓存了所有要使用的DOM元素。 Without looking too deep into jQuery's source code, is there a chance that my performance issues are located there? 在不深入研究jQuery源代码的情况下,是否有可能出现我的性能问题?

Does jQuery use DocumentFragments to append things? jQuery是否使用DocumentFragments附加内容?

If you create a new DOM element with 如果您使用创建新的DOM元素

var new = $('<div/>') 

it is only stored in memory at this point isnt it? 它现在只存储在内存中吗?

If you think about performance, you should construct you whole <ul> element with all <li> subelements as a string. 如果您考虑性能,则应将所有<li>子元素作为字符串构造整个<ul>元素。 Use " += " operation for the string concatenations. 对字符串串联使用“ + = ”操作。 At the end use jQuery to insert constructed string inside your web page. 最后,使用jQuery在网页内插入构造的字符串。

See http://www.sitepen.com/blog/2008/05/09/string-performance-an-analysis/ about string performance. 有关字符串性能的信息,请参见http://www.sitepen.com/blog/2008/05/09/string-performance-an-analysis/

There is a slide from John Resig (jQuery creator) where he says that jQuery does in fact use document fragments internally. John Resig (jQuery的创建者)有一张幻灯片 ,他说jQuery实际上确实在内部使用文档片段。 On the following slides he explains briefly how to build up a lightweight document fragment and append it in one operation stating that this " ends up being really fast ". 在下面的幻灯片中,他简要地解释了如何构建一个轻量级的文档片段,并将其附加到一个操作中,说明此操作“ 最终变得非常快 ”。 You might want to look into that, if you did not already (as you mention document fragments yourself). 如果您还没有这样做的话,您可能想研究一下(正如您自己提到的文档片段)。

As for the process of creating new elements with $('<div>') , the documentation says: 至于使用$('<div>')创建新元素的过程, 文档说:

But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. 但是,如果该字符串看起来像是HTML代码段,则jQuery尝试创建HTML所描述的新DOM元素。 Then a jQuery object is created and returned that refers to these elements. 然后,创建并返回一个引用这些元素的jQuery对象。

jQuery uses "the native JavaScript createElement() function" if you provide a single tag and "the browser's innerHTML mechanism" for more complex html snippets. 如果您提供单个标记,则jQuery使用“本机JavaScript createElement()函数”,而对于更复杂的html代码段,则使用“浏览器的innerHTML机制”。

I had the same problem just a few days ago. 几天前我也遇到了同样的问题。 Its much faster if you do it this way: 如果这样做的话,速度会更快:

var buffer = $('<ul class="custom-example" style="position: absolute; top:500px"></ul>')

var html = ''

$.each(pages[pindex], function(i, v){
   html += v
});

buffer.html(html)

buffer.insertAfter($root);

(be careful: in your example buffer is already a jquery object, you don't need to reslect it for the insertAfter) (注意:在您的示例缓冲区中,缓冲区已经是一个jquery对象,您无需为insertAfter更改它)

Creating DOM nodes one by one is usually slower than creating a HTML string and let the browser process it (via innerHTML). 通常一一创建DOM节点要比创建HTML字符串并使浏览器(通过innerHTML)对其进行处理要慢。 Here are more details about this. 这是有关此的更多详细信息。

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

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