[英]Javascript What is faster? Multiple OR in IF statements to target css, or one IF with multiple css targets
I have set some code to function if the current page contains one of the following attributes below 如果当前页面包含以下以下属性之一,则我已设置了一些代码以使其起作用
if($('body[data-section="chicken"]').length || $('body[data-section="friedchicken"]').length){
(function() {
///...
}
}
Now i could simply do it like: 现在我可以像这样简单地做:
if($('body[data-section="chicken"],body[data-section="friedchicken"]').length){
(function() {
///...
}
}
Basically, a function to auto scroll down on the targeted pages. 基本上是一种自动向下滚动目标页面的功能。 But i'm curious!
但是我很好奇! which one is faster to detect these targets?
哪一个检测这些目标的速度更快? and the reason I'm asking because i will be using more than 2 css selectors.
我问的原因是因为我将使用2个以上的CSS选择器。
If speed is a concern, then skip the jQuery layer, address the body
element directly, and only query for its data-section attribute once: 如果需要考虑速度,则跳过jQuery层,直接处理
body
元素,只查询一次data-section属性:
var attr = document.body.getAttribute('data-section');
if (attr == 'chicken' || attr == 'friedchicken') {
// ...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.