性能:选择器还是遍历? [英] Performance: selector or traversing?

查看:73
本文介绍了性能:选择器还是遍历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道哪种是在身体上应用课程的最快方法,比方说,所有段落。我们可以使用简单的选择器,例如:

  $(body)。addClass(foo); 
$(p)。addClass(bar);

但也遍历如下方法:



<$ p 。$ p> $( 正文)addClass( 富)找到( p)addClass( 条)。

表现有任何差异吗?



干杯,
本杰明

解决方案

我怀疑第二版会稍微快一点因为 find 将在前一个选择器结果的子元素中查找匹配选择器的元素。但是在这种情况下,差异可能会很小,你甚至都不会注意到它。


I was wondering which was the fastest way to apply a class on body and, let's say, all paragraphs. We can use simple selectors such as:

$("body").addClass("foo");
$("p").addClass("bar");

But also traversing methods like:

$("body").addClass("foo").find("p").addClass("bar");

Is there any difference in term of performance?

Cheers, Benjamin

解决方案

I would suspect second version to be slightly faster because find will look for the elements matching selector among the children of the previous selector results. However in this case the difference will probably be so small that you won't even notice it.

这篇关于性能:选择器还是遍历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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