jQuery查找性能 [英] JQuery Find Performance
问题描述
我发现这是
var $colSelect = $('#stuff').find('.things');
$($colSelect).find('img, input, select').addClass("foo");
比这要慢:
var $colSelect = $('#stuff').find('.things');
$($colSelect).find('img').addClass("foo");
$($colSelect).find('input').addClass("foo");
$($colSelect).find('select').addClass("foo");
有人可以帮助我理解为什么会这样吗?具体来说,我注意到IE8的性能有所提高.
Can anyone help me understand why that is the case? In specific I am noticing the performance increase in IE8.
推荐答案
我的猜测是,对于第一个选择器,jQuery必须解析该选择器,为每个选择器调用JavaScript的getElementsByTagName
并加入结果(甚至可以对它们进行排序)他们在DOM中的位置顺序.
My guess would be that for the first selector, jQuery must parse the selector, call JavaScript's getElementsByTagName
for each and join the results (maybe even ordering them in the order of their position in the DOM).
第二种情况是对JavaScript的getElementsByTagName
的直接调用,而无需其他操作.
The second case has straight-forward calls to JavaScript's getElementsByTagName
, with no other operations necessary.
此页面比较性能: http://jsperf.com/jquery-selector-performance-problems
这篇关于jQuery查找性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!