Suppose I want to get the first element amongst all the elements of the class ".answer"
假设我想获得“.answer”类中所有元素中的第一个元素
$($(".answer")[0])
I can do the above, but what is the best balance between elegance and speed?
我可以做到这一点,但优雅和速度之间的最佳平衡是什么?
*changed the question to reflect the current discussion
*改变了问题以反映当前的讨论
3 个解决方案
#1
56
The following are all equivalent in functionality (though not speed):
以下都是功能相同的(虽然不是速度):
var a0 = $($('.answer')[0]);
- var a0 = $($('。answer')[0]);
-
var a0 = $('.answer').first();
- see http://api.jquery.com/first/ - var a0 = $('。answer')。first(); - 见http://api.jquery.com/first/
-
var a0 = $('.answer:first');
- see http://api.jquery.com/first-selector/ - var a0 = $('。answer:first'); - 见http://api.jquery.com/first-selector/
-
var a0 = $('.answer').eq(0);
- see http://api.jquery.com/eq/ - var a0 = $('。answer')。eq(0); - 见http://api.jquery.com/eq/
-
var a0 = $('.answer:eq(0)');
- see http://api.jquery.com/eq-selector/ - var a0 = $('。answer:eq(0)'); - 请参阅http://api.jquery.com/eq-selector/
Which is the best?
It has been hypothesized that the selector versions should be faster than the method versions (and the logic makes some sense) but I have not yet found a reliable cross-browser, multi-document benchmark that proves this to be true.
哪个最好?已经假设选择器版本应该比方法版本更快(并且逻辑有一定意义)但我还没有找到可靠的跨浏览器,多文档基准测试,证明这是真的。
And in some cases you cannot use the selector, as you have a jQuery object resulting from chained results and must later pare it down.
在某些情况下,您不能使用选择器,因为您有一个由链接结果产生的jQuery对象,并且必须在以后削减它。
Edit: Based on the excellent information from @yc's tests below, following are the current (2011-Feb-4) test results summarized and compared against a baseline of .answer:first
:
编辑:根据以下@ yc测试的优秀信息,以下是当前(2011年2月4日)测试结果的总结,并与.answer基线进行比较:首先:
:first :eq(0) .first() .eq(0) $($('...')[0]) Chrome 8+ 100% 92% 224% 266% 367% FF 3.6 100% 100% 277% 270% 309% FF 4.0b 100% 103% 537% 521% 643% Safari 5 100% 93% 349% 352% 467% Opera 11 100% 103% 373% 374% 465% IE 8 100% 101% 1130% 1246% 1767% iPhone 4 100% 95% 269% 316% 403% ===================================================== Weighted 100% 92% 286% 295% 405% Major 100% 95% 258% 280% 366%
- The Weighted line shows the performance weighted by the number of tests per browser; popular browsers (among those testing) are counted more strongly.
- 加权线显示每个浏览器的测试次数加权的性能;流行的浏览器(在那些测试中)被更强烈地计算。
- The Major line shows the same, only including non-beta releases of the major desktop browsers.
- Major系列显示相同,仅包括主要桌面浏览器的非beta版本。
In summary: the hypothesis is (currently) wrong. The methods are significantly faster than the Sizzle selectors, and with almost no exception the OP's code $($('.answer')[0])
is the fastest of them all!
总之:假设(目前)是错误的。这些方法明显快于Sizzle选择器,并且几乎没有例外,OP的代码$($('。answer')[0])是最快的!
#2
73
I can't speak to the elegance aspect, but the performance aspect here actually can make a huge difference.
我不能说优雅的方面,但这里的表现方面实际上可以产生巨大的差异。
It looks like, from a set of JavaScript testing, that your original method is actually the most efficient one, and contrary to the hypothesizing that the accepted answer linked to, non-CSS Sizzle selectors tend to be much less efficient than method selectors. There's a reason for that. The $('.answer')
can use the browser native getElementsByClass()
without having to manually traverse the results. The :first
selector complicates that. In this instance, using the sizzle selectors seems to slow the selection by a factor of between 4-5.
看起来,从一组JavaScript测试中,您的原始方法实际上是最有效的方法,并且与假设所接受的答案相关联,非CSS Sizzle选择器往往比方法选择器效率低得多。这是有原因的。 $('。answer')可以使用浏览器本机getElementsByClass(),而无需手动遍历结果。 :第一选择器使其复杂化。在这种情况下,使用嘶嘶声选择器似乎会使选择速度减慢4-5倍。
I'd argue that, with jQuery, performance should trump elegance, and all evidence (every single browser I've tested so far!) seems to indicate that OP's inelegant solution is the fastest by a fair amount.
我认为,使用jQuery,性能应该胜过优雅,所有证据(到目前为止我测试过的每一个浏览器!)似乎都表明OP的优雅解决方案是最快的。
Here are the results of the browsers with the most test runs:
以下是测试运行次数最多的浏览器的结果:
(The numbers are 'operations per second', so higher numbers are faster, lower numbers are slower.)
(这些数字是'每秒操作数',所以数字越高,数字越慢。)
#3
#1
56
The following are all equivalent in functionality (though not speed):
以下都是功能相同的(虽然不是速度):
var a0 = $($('.answer')[0]);
- var a0 = $($('。answer')[0]);
-
var a0 = $('.answer').first();
- see http://api.jquery.com/first/ - var a0 = $('。answer')。first(); - 见http://api.jquery.com/first/
-
var a0 = $('.answer:first');
- see http://api.jquery.com/first-selector/ - var a0 = $('。answer:first'); - 见http://api.jquery.com/first-selector/
-
var a0 = $('.answer').eq(0);
- see http://api.jquery.com/eq/ - var a0 = $('。answer')。eq(0); - 见http://api.jquery.com/eq/
-
var a0 = $('.answer:eq(0)');
- see http://api.jquery.com/eq-selector/ - var a0 = $('。answer:eq(0)'); - 请参阅http://api.jquery.com/eq-selector/
Which is the best?
It has been hypothesized that the selector versions should be faster than the method versions (and the logic makes some sense) but I have not yet found a reliable cross-browser, multi-document benchmark that proves this to be true.
哪个最好?已经假设选择器版本应该比方法版本更快(并且逻辑有一定意义)但我还没有找到可靠的跨浏览器,多文档基准测试,证明这是真的。
And in some cases you cannot use the selector, as you have a jQuery object resulting from chained results and must later pare it down.
在某些情况下,您不能使用选择器,因为您有一个由链接结果产生的jQuery对象,并且必须在以后削减它。
Edit: Based on the excellent information from @yc's tests below, following are the current (2011-Feb-4) test results summarized and compared against a baseline of .answer:first
:
编辑:根据以下@ yc测试的优秀信息,以下是当前(2011年2月4日)测试结果的总结,并与.answer基线进行比较:首先:
:first :eq(0) .first() .eq(0) $($('...')[0]) Chrome 8+ 100% 92% 224% 266% 367% FF 3.6 100% 100% 277% 270% 309% FF 4.0b 100% 103% 537% 521% 643% Safari 5 100% 93% 349% 352% 467% Opera 11 100% 103% 373% 374% 465% IE 8 100% 101% 1130% 1246% 1767% iPhone 4 100% 95% 269% 316% 403% ===================================================== Weighted 100% 92% 286% 295% 405% Major 100% 95% 258% 280% 366%
- The Weighted line shows the performance weighted by the number of tests per browser; popular browsers (among those testing) are counted more strongly.
- 加权线显示每个浏览器的测试次数加权的性能;流行的浏览器(在那些测试中)被更强烈地计算。
- The Major line shows the same, only including non-beta releases of the major desktop browsers.
- Major系列显示相同,仅包括主要桌面浏览器的非beta版本。
In summary: the hypothesis is (currently) wrong. The methods are significantly faster than the Sizzle selectors, and with almost no exception the OP's code $($('.answer')[0])
is the fastest of them all!
总之:假设(目前)是错误的。这些方法明显快于Sizzle选择器,并且几乎没有例外,OP的代码$($('。answer')[0])是最快的!
#2
73
I can't speak to the elegance aspect, but the performance aspect here actually can make a huge difference.
我不能说优雅的方面,但这里的表现方面实际上可以产生巨大的差异。
It looks like, from a set of JavaScript testing, that your original method is actually the most efficient one, and contrary to the hypothesizing that the accepted answer linked to, non-CSS Sizzle selectors tend to be much less efficient than method selectors. There's a reason for that. The $('.answer')
can use the browser native getElementsByClass()
without having to manually traverse the results. The :first
selector complicates that. In this instance, using the sizzle selectors seems to slow the selection by a factor of between 4-5.
看起来,从一组JavaScript测试中,您的原始方法实际上是最有效的方法,并且与假设所接受的答案相关联,非CSS Sizzle选择器往往比方法选择器效率低得多。这是有原因的。 $('。answer')可以使用浏览器本机getElementsByClass(),而无需手动遍历结果。 :第一选择器使其复杂化。在这种情况下,使用嘶嘶声选择器似乎会使选择速度减慢4-5倍。
I'd argue that, with jQuery, performance should trump elegance, and all evidence (every single browser I've tested so far!) seems to indicate that OP's inelegant solution is the fastest by a fair amount.
我认为,使用jQuery,性能应该胜过优雅,所有证据(到目前为止我测试过的每一个浏览器!)似乎都表明OP的优雅解决方案是最快的。
Here are the results of the browsers with the most test runs:
以下是测试运行次数最多的浏览器的结果:
(The numbers are 'operations per second', so higher numbers are faster, lower numbers are slower.)
(这些数字是'每秒操作数',所以数字越高,数字越慢。)