有没有办法加速这个解决方案不区分大小写的jQuery:包含选择器? [英] Is there any way to speed up this solution for a case-insensitive jQuery :contains selector?
问题描述
我找到了此解决方案一个不区分大小写的jQuery :在StackOverflow上包含
选择器。它运行良好,但它以性能为代价。有没有其他人觉得这个解决方案有点慢?
I found this solution for a case-insensitive jQuery :contains
selector on StackOverflow. It works great, however it comes at the cost of performance. Does anyone else find this solution to be a bit slow?
我正在使用:contains
选择器进行搜索一张桌子。用户在文本框中键入搜索字符串。对于每次击键,它会在表中搜索该字符串,仅通过:contains
选择器显示包含该字符串的行。在实施不区分大小写的解决方案之前,此搜索快速而且快速。现在有了这个解决方案,它会在每次击键后锁定一小段时间。
I'm using the :contains
selector to search a table. 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. 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?
推荐答案
我在Google上找到了另一种不区分大小写搜索的解决方案(参见 Eric Phan )与我最初使用的版本略有不同。
I found another solution of the case-insensitive search on Google (see Eric Phan) which varies slightly from the one I was originally using.
原文:
return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
EricPhan:
return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase())>=0;
比较两者,你可以看到Eric Phan的解决方案直接访问DOM属性并使用 toLowerCase()
而不是 toUpperCase()
。后者并不重要,但前者真正改善了不区分大小写搜索的性能。只需将原始解决方案更改为使用(a.textContent || a.innerText ||)
而不是 jQuery(a).text()
完全不同!
Comparing the two, you can see Eric Phan's solution accesses the DOM attributes directly and uses toLowerCase()
instead of 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!
现在我有点好奇,所以这是一个后续问题:与$ code> jQuery.text()$ c $的交易是什么C>?为什么这么慢?我有我的假设,但我很想听听专家们的意见。
Now I'm a bit curious, so here's a follow up question: What's the deal with 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. =)
这篇关于有没有办法加速这个解决方案不区分大小写的jQuery:包含选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!