jQuery选择器:为什么$(“#id").find("p")比$(“#id p")更快 [英] jQuery selector: Why is $("#id").find("p") faster than $("#id p")

查看:162
本文介绍了jQuery选择器:为什么$(“#id").find("p")比$(“#id p")更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此页的作者: http://24ways.org/2011/your-jquery-now-with-suck- 断言jQuery选择器 $('#id').find('p')$('#id p')快,尽管如果我理解正确的话,它大概会产生相同的结果.造成这种差异的原因是什么?

The author of this page: http://24ways.org/2011/your-jquery-now-with-less-suck asserts that the jQuery selector $('#id').find('p') is faster than $('#id p'), although that presumably produce the same results if I understand correctly. What is the reason for this difference?

推荐答案

因为$('#id').find('p')已被优化以执行...

Because $('#id').find('p') is optimized to do...

document.getElementById('id').getElementsByTagName('p');

...我猜想$('#id p')将使用querySelectorAll(如果可用),或者使用基于JavaScript的选择器引擎.

...whereas I'm guessing $('#id p') will either use querySelectorAll if available, or the JavaScript based selector engine if not.

您应该注意,浏览器之间的性能总是会有差异的. Opera的querySelectorAll速度非常快.

You should note that performance always has variations between browsers. Opera is known to have an extremely fast querySelectorAll.

此外,不同版本的jQuery可能会有不同的优化.

Also, different versions of jQuery may come up with different optimizations.

给出与第一个版本相同的优化结果,$('#id p')可能是(或当前是).

It could be that $('#id p') will be (or currently is) given the same optimization as the first version.

这篇关于jQuery选择器:为什么$(“#id").find("p")比$(“#id p")更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆