有人能打败这个 jQuery 选择器吗?
我一直在运行一些测试,看看是否可以找到一个有效的控件选择器,该控件的前缀为 Asp.Net 中父控件的 id
我一直在寻找这个,因为我能够从外部 javascript 文件中选择 Asp 控件(我厌倦了使用 ClientID)。
为了进行测试,我设置了一个包含 150 个文本框的页面,所有文本框均具有 cssclass“speedy”和单独的 id,并运行以下代码来选择第 107 个文本框。
function testclientInput() {
var iterations = 100;
var totalTime = 0;
// Record the starting time, in UTC milliseconds.
var start = new Date().getTime();
// Repeat the test the specified number of iterations.
for (i = 0; i < iterations; i++) {
// Execute the selector. The result does not need
// to be used or assigned to determine how long
// the selector itself takes to run.
// All tests done in ie6, ie7, ie8, ie9beta, firefox3.6, opera11 & chrome8
// slowest
// $('input.speedy[id$=_TextBox107]');
// Quick but only useful if you know the index.
//$($('input.speedy')[106]);
//$('[id$=_TextBox107]:first');
//$('input[id$=_TextBox107]');
//$.clientID("TextBox107");
//$('[id$=_TextBox107]');
//$('input[id$=_TextBox107]:first');
//$($('[id$=_TextBox107]')[0]);
// Fastest
//$($('input[id$=_TextBox107]')[0]);
}
// Record the ending time, in UTC milliseconds.
var end = new Date().getTime();
// Determine how many milliseconds elapsed
totalTime = (end - start);
// Report the average time taken by one iteration.
alert(totalTime / iterations);
};
这是我想出的最好的办法。 $($('输入[id$=_TextBox107]')[0]);
。结果令人惊讶......我期望使用 :first
选择器来匹配我的版本,但它报告的结果要慢得多。
有人能想出一种方法来优化这个吗?
I've been running some tests to see if I can find an efficient selector for controls prefixed with the id of parent controls in Asp.Net
I've been looking for this as I was to be able to select Asp controls from external javascript files (I'm fed up of using ClientID).
To test I set up a page with 150 textboxes all with the cssclass "speedy" and an individual id and ran the following code to select the 107th textbox.
function testclientInput() {
var iterations = 100;
var totalTime = 0;
// Record the starting time, in UTC milliseconds.
var start = new Date().getTime();
// Repeat the test the specified number of iterations.
for (i = 0; i < iterations; i++) {
// Execute the selector. The result does not need
// to be used or assigned to determine how long
// the selector itself takes to run.
// All tests done in ie6, ie7, ie8, ie9beta, firefox3.6, opera11 & chrome8
// slowest
// $('input.speedy[id$=_TextBox107]');
// Quick but only useful if you know the index.
//$($('input.speedy')[106]);
//$('[id$=_TextBox107]:first');
//$('input[id$=_TextBox107]');
//$.clientID("TextBox107");
//$('[id$=_TextBox107]');
//$('input[id$=_TextBox107]:first');
//$($('[id$=_TextBox107]')[0]);
// Fastest
//$($('input[id$=_TextBox107]')[0]);
}
// Record the ending time, in UTC milliseconds.
var end = new Date().getTime();
// Determine how many milliseconds elapsed
totalTime = (end - start);
// Report the average time taken by one iteration.
alert(totalTime / iterations);
};
This is the best I have come up with. $($('input[id$=_TextBox107]')[0]);
. The results have been surprising.... I expected using the :first
selector to match my version but it reported much slower results.
Can anyone think of a way to optimize this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将取决于浏览器。
此版本:
...是一个有效的
querySelectorAll
选择器,因此任何实现它的浏览器都会非常快。如果您使用
:first
选择器,则您使用的不是有效的 CSS 选择器,因此它默认使用 Sizzle 基于 javascript 的选择器引擎,并且速度会慢得多。如果性能至关重要,那么就不要使用 jQuery。如果浏览器不支持
querySelectorAll
,您只需使用document.getElementsByTagName()
即可过滤所需的元素。It will depend on the browser.
This version:
...is a valid
querySelectorAll
selector, so any browser that implements it will be very fast.If you use the
:first
selector, you're using something that is not a valid CSS selector, so it defaults to Sizzle's javascript based selector engine, and will be much slower.If performance is crucial, then don't use jQuery for this. You can just use
document.getElementsByTagName()
, and filter the one(s) you want, if the browser doesn't supportquerySelectorAll
.我没有进行任何详尽的测试,但我使用“最慢”选择器得到的结果或多或少与“最快”选择器一样快......但其他一些选项明显较慢。然而,我只使用 IE8 进行了测试......
在阅读本文之前我选择的方法是......
它的执行速度比你的“最快”要慢一些,但到目前为止还不是最糟糕的选择。我可能需要做更多测试并重新考虑我的选择。
I didnt do any exhaustive testing but I was getting results using your "slowest" selector that were more or less as fast as your "fastest" selector...Some of the other options were noticeably slower though. However, I only tested using IE8...
My method of choice before reading this was...
which performed somewhat slower that your "fastest" but was by far not the worst choice. I may have to do some more testing and rethink my choice.