简体   繁体   English

正确使用以下语句性能的正确方法是哪种?

[英]Which is right way to use below statement performance wise?

Which approach is most recommended. 最推荐哪种方法。 First or Second ? 第一还是第二?

var x = document.querySelectorAll("span");

// #1:  Console returns false
x.length > 0 && x[0].blur();

// #2:  Console returns undefined
if (x.length > 0) {
  x[0].blur();
}

If you find x.length > 0 then you will definitely get x[0] and do not need to check && x[0].blur(); 如果发现x.length > 0那么您肯定会得到x[0]并且不需要检查&& x[0].blur(); . So your second approach is much better. 因此,第二种方法要好得多。

if (x.length > 0) {
  x[0].blur();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这是使用if语句的正确方法吗? - is this the right way to use if statement? 我可以在以下任何问题中使用“switch”语句吗? 或者'else if'是go的正确方法吗? - can I use the 'switch' statement in any of the questions below? or is the 'else if' the right way to go? 在下面的列表中哪一个用于.click的性能更好 - Which one is better performance to use for .click in the below list 是否更好地使用.delegate()性能? - Is it better to use .delegate() performance wise? jQuery on()方法-哪种使用方式对性能更好? - jQuery on() method - which way to use is better for performance? 在这种特定情况下,if-else 是否比 try-catch 更好,性能方面? 哪种方式是最佳实践? - Is if-else better than try-catch in this particular scenario, performance wise? Which way is the best practice? 移动元素的最佳方法(性能明智) - Best way to move an element around (performance wise) 如何正确使用Input占位符? 是否正在尝试对占位符使用转换,但不起作用? 屏幕截图如下 - How to use Input placeholder in a right way? Am trying to use transforms to placeholders but it does not work? screenshot is below 就 electron 中使用 IPC 侦听器的性能而言,哪一种方法更好? - Which one is better way in terms of performance to use IPC listener in electron? 这些ArrayToMap函数用法中的哪一种在性能方面更好? - Which one of these ArrayToMap functions usages is better performance-wise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM