更快$(“s1”)。find(“s2”)。find(“s3”)或$(“s1 s2 s3”)? [英] What's faster $("s1").find("s2").find("s3") or $("s1 s2 s3")?

查看:163
本文介绍了更快$(“s1”)。find(“s2”)。find(“s3”)或$(“s1 s2 s3”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当选择某些DOM元素时,jQuery是多方面的。今天,我注意到获得相同元素的两种方式可能产生不同的速度:

  $(selector1) .find(selector2)。find(selector3)

  $(selector1 selector2 selector3)

(其中 selectorX 可以是ID或类或其他任何东西)



的元素,但有没有速度的差异? jQuery如何实际遍历DOM?在第二种情况下,这是非常重要的:它是从 selector1到selector3 还是



使用单个 $('...') )的速度是Chrome的大约两倍,比如链接 $。find() s。这个 JSPerf Benchmark 会让您更好地了解其他浏览器,以及一些更多人测试它。


jQuery is quite multifaceted when it comes to selecting certain DOM elements. Today it came to my attention that the two ways of getting the same elements may yield different speed:

$("selector1").find("selector2").find("selector3")

and

$("selector1 selector2 selector3")

(where selectorX can be ID or class or anything else)

Both produce the same set of elements but are there any speed differences? How does jQuery actually traverse DOM? This is especially important in the second case: does it go from selector1 to selector3 or the other way around?

Anybody measured the difference between the two?

解决方案

Using a single $('...') is about twice as fast as chaining $.find()s for me in Chrome. This JSPerf Benchmark will give you a good idea of other browsers as well once some more people test it.

这篇关于更快$(“s1”)。find(“s2”)。find(“s3”)或$(“s1 s2 s3”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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