简体   繁体   English

使用jQuery按值选择选项的性能更好吗?

[英]Better performance for selecting options by value with jQuery?

I am trying to work out some performance problems with some JavaScript I've been working on for a few days. 我正在尝试使用几天来开发的JavaScript解决一些性能问题。 One of the pieces of the functions is below: 这些功能之一如下:

var removeAddress = function(pk) {
    var startTime = new Date();
    jQuery('.add_driver select.primary_address:has(option[value=' + pk + ']:selected)').each(function(c, o) {
        console.log("Shouldn't get here yet...");
        showInputs(o);
    });
    console.log('removeAddress1:  ', (new Date() - startTime) / 1000);
    jQuery('.add_driver select.primary_address option[value=' + pk + ']').remove();
    console.log('removeAddress2:  ', (new Date() - startTime) / 1000);
};

This code is quite peppy in Firefox: 这段代码在Firefox中非常刺激:

removeAddress1:  0.004
removeAddress2: 0.023

But in IE8 it is another story: 但是在IE8中,这是另一个故事:

LOG: removeAddress1: 0.203
LOG: removeAddress2: 0.547

The form in question is a 20-person in put form with first name, last name, and 5 address fields. 有问题的表单是一个20人的表单,其中有名字,姓氏和5个地址字段。 I've also put in a drop down for selecting other addresses already existing in the form ( .primary_address ). 我还添加了一个下拉列表,用于选择其他形式( .primary_address )已存在的地址。 This code is removing an address from the primary address select boxes. 该代码正在从主地址选择框中删除一个地址。

I'm trying to nail down why this is taking so long, and the only thing which stands out is the option[value=????] section. 我试图确定为什么要花这么长时间,而唯一引人注意的是option[value=????]部分。 This was the most practical way to find the elements in question, so I ran with it. 这是查找有问题的元素的最实用方法,因此我同意了。 Is there something about these two selectors which is causing IE to lose its lunch? 关于这两个选择器,是否导致IE失去午餐?

The option element is always temperamental. 选项元素始终是气质。 Perhaps it's simpler to just get all the SELECT elements and then simply query their values. 也许只获取所有SELECT元素然后简单地查询它们的值会更简单。 The selected OPTION always will give its value property to the SELECT as well. 选定的OPTION始终还将其value属性也赋予SELECT。 So: 所以:

jQuery('.add_driver select.primary_address').filter(function() {
  return $(this).value === pk;
});

jQuery('.add_driver select.primary_address[value='+pk+']');

Maybe one of those will be faster - not sure if the second will work. 也许其中一种会更快-不确定第二种是否会工作。

You can likely speed this up a lot by breaking down your uber-selector string. 您可以通过分解超级选择器字符串来大大加快这一步。

To start, begin with an id, or even better a cached element. 首先,以id或更好的缓存元素开始。 Then get your select elements using .children() . 然后使用.children()获取select元素。 Instead of using the :has selector use .has() . 不要使用:has选择器,而要使用.has() Methods are generally faster than complex selector syntax because jQ doesn't have to parts a string to figure out what you mean. 方法通常比复杂的选择器语法快,因为jQ不必分割字符串即可弄清楚您的意思。 Then, as Rafael said, you can skip the :selected and just look at the value of the matched select 's. 然后,正如拉斐尔所说,您可以跳过:selected而只需查看匹配的select的值。

formElem = document.getElementById('formId');
jQuery('.add_driver', formElem)
    .children('select.primary_address')
        .has('[value=' + pk + ']')
        .each(...);

Passing formElem as the second arg uses it as the context for the search so jQ doesn't have to start at the root. formElem作为第二个参数传递formElem ,将其用作搜索的上下文,因此jQ不必从根开始。

To .remove() the elements either cache the jQuery object from above or chain it on after the .each() so you don't have to reselect everything again. .remove() ,元素可以从上方缓存jQuery对象,也可以在.each()之后将其.each()因此您不必再次选择所有内容。

也许在removeAddress函数之外预先计算$('formId .add_driver select'),然后重用该代码,这样removeAddress()不必枚举太多元素。

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

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