简体   繁体   English

有没有办法加速这个解决方案不区分大小写的jQuery:包含选择器?

[英]Is there any way to speed up this solution for a case-insensitive jQuery :contains selector?

I found this solution for a case-insensitive jQuery :contains selector on StackOverflow. 我找到了一个不区分大小写的jQuery的解决方案 :contains在StackOverflow上:contains选择器。 It works great, however it comes at the cost of performance. 它运行良好,但它以性能为代价。 Does anyone else find this solution to be a bit slow? 有没有其他人觉得这个解决方案有点慢?

I'm using the :contains selector to search a table. 我正在使用:contains选择器来搜索表。 The user types a search string into a textbox. 用户在文本框中键入搜索字符串。 For each keystroke, it searches the table for that string, showing only the rows that contain that string via the :contains selector. 对于每个击键,它在表中搜索该字符串,仅通过:contains选择器显示包含该字符串的行。 Before implementing the case-insensitive solution, this search was quick and snappy. 在实施不区分大小写的解决方案之前,此搜索快速而且快速。 Now with this solution, it locks up for a brief moment after each keystroke. 现在有了这个解决方案,它会在每次击键后锁定一小段时间。

Any ideas on how this solution could be sped up? 关于如何加快这个解决方案的任何想法?

I found another solution of the case-insensitive search on Google (see Eric Phan ) which varies slightly from the one I was originally using. 我在Google上找到了另一种不区分大小写搜索的解决方案(参见Eric Phan ),这与我最初使用的解决方案略有不同。

Original: 原版的:

return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;

EricPhan: EricPhan:

return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase())>=0;

Comparing the two, you can see Eric Phan's solution accesses the DOM attributes directly and uses toLowerCase() instead of toUpperCase() . 比较两者,您可以看到Eric Phan的解决方案直接访问DOM属性并使用toLowerCase()而不是toUpperCase() The latter doesn't really matter, but the former is what really improved the performance of the case-insensitive search. 后者并不重要,但前者真正改善了不区分大小写搜索的性能。 Just changing the original solution to use (a.textContent || a.innerText || "") instead of jQuery(a).text() made all the difference! 只需更改原始解决方案(a.textContent || a.innerText || "")而不是jQuery(a).text()就可以了!

Now I'm a bit curious, so here's a follow up question: What's the deal with jQuery.text() ? 现在我有点好奇,所以这是一个跟进问题: jQuery.text()的处理是什么? Why's it so slow? 为什么这么慢? I have my assumptions, but I'd love to hear what the experts have to say. 我有我的假设,但我很想听听专家们的意见。

Lastly, thanks to everyone who responded. 最后,感谢所有回复的人。 I appreicate your help. 我恭维你的帮助。 =) =)

You could try to check the selector only once, after the user has stopped typing for a specified amount of time, not for every keystroke. 在用户停止键入指定的时间后,您可以尝试仅检查选择器一次,而不是每次按键。

For example, a simple implementation: 例如,一个简单的实现:

Usage: 用法:

$("#textboxId").keyup(function () {
  typewatch(function () {
    // executed only 500 ms after the user stopped typing.
  }, 500);

Implementation: 执行:

var typewatch = function(){
    var timer = 0;  // store the timer id
    return function(callback, ms){
        clearTimeout (timer);  // if the function is called before the timeout
        timer = setTimeout(callback, ms); // clear the timer and start it over
    }  
}();

you could try not checking after each keystroke, but maybe a second after the last keystroke has been pressed. 您可以尝试在每次按键后不进行检查,但可能在按下最后一次按键后一秒钟。 this way you're not constantly checking while the user is typing but rather checking when the user is finished or pausing typing. 这样你就不会在用户输入时不断检查,而是检查用户何时完成或暂停键入。

here's a follow up question: What's the deal with jQuery.text()? 这是一个后续问题:jQuery.text()的处理是什么? Why's it so slow? 为什么这么慢?

I suspect that it's slow due to the $(a) (converting the DOM element to a jQuery object) and not the .text() . 由于$(a) (将DOM元素转换为jQuery对象)而不是.text()我怀疑它很慢。

要添加到jason所说的内容,您可以尝试使用此插件来实现这一目标。

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

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