选择器的性能 [英] Performance of selector
本文介绍了选择器的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经对 jQuery 选择器的性能进行了测试.
I've done a test with performance of a jQuery selector.
我测试过的两个选择器:
The two selectors I have tested:
selection_width = total_width - ($('#commands .minimap').outerWidth() + $('#commands .actions').outerWidth());
还有:
var commands = $('#commands');
selection_width = total_width - ($('.minimap', commands).outerWidth() + $('.actions', commands).outerWidth());
第二个比第一个快得多.这是正确的还是我在某处搞砸了测试?
The second one is way faster than the first one. Is this correct or did I screw up the test somewhere?
推荐答案
在第一次测试中,jQuery 使用了 document.querySelectorAll()
两次(相对较快).在第二种情况下,jQuery 使用 document.getElementById()
(非常快)一次和 document.getElementsByClassName()
(因为你声明了一个上下文,所以很快)两次.
In the first test, jQuery uses document.querySelectorAll()
(relatively fast) twice. In the second case, jQuery uses document.getElementById()
(very fast) once and document.getElementsByClassName()
(fast since you declared a context) twice.
这篇关于选择器的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文