问题描述
在比较两个相同类型的操作数时是否相等JavaScript,使用 ==
或 ===
并没有什么概念上的区别,所以我想知道哪个运算符实际上,在必须从远程Internet位置下载JavaScript文件的情况下,它实际上要更快。
而严格相等运算符 = ==
在许多用户代理上的执行速度可能更快,并且还需要8位以上的未压缩信息与JavaScript文件一起在网络上传输。
碰巧的是,今天的平均CPU执行数百次条件跳转的速度比Internet连接传递一位要快得多,所以我很想使用 ==
代替 ===
和!=
代替!==
。但是,我对阅读如此众多建议采取相反做法的博客感到困惑。
我是否缺少任何重要的观点?
=== 相比,使用
==
的文件大小略有优势。 / p> 但是,有人认为一致性更重要: ===
通常更接近于测试时的预期相等,并且仅使用 ===
和!==
是很多人认为有用和可读的东西。就我个人而言,我有相反的规则,当操作数类型不确定时,仅使用 ===
,但我不建议在其他方法中使用任何一种方法。 / p>
如果您了解严格和非严格相等之间的区别,并且相信使用 ==
和!=
不会使您或您其他任何代码的人在将来阅读和理解代码时遇到任何问题,请继续使用它们。
When comparing two operands of the same type for equality in JavaScript, using ==
or ===
doesn't make any conceptual difference, so I'm wondering which operator is actually faster when, like in my case, a JavaScript file has to be downloaded from a remote Internet location.
While the strict equality operator ===
may perform faster on many user agents, it also requires 8 more bits of uncompressed information to be carried along the network with the JavaScript file.
As it happens, today's average CPUs are much faster in executing several hundred conditional jumps than Internet connections are in delivering one single bit, so I'd be keen to using ==
instead of ===
and !=
instead of !==
when possible. Yet I'm confused by reading so many blogs that recommend doing the opposite.
Is there any important point I'm missing?
As you say, for comparison where both operands are guaranteed to be of the same type, the two operators are specified to perform precisely the same steps and should (and I think do) perform near enough identically. Therefore there is a slight advantage in terms of file size in using ==
over ===
in those cases.
However, some people argue that consistency is more important: ===
is usually closer to what you intend when testing equality, and only using ===
and !==
is something many people find helpful and readable. Personally, I have the opposite rule and only use ===
when there is uncertainty about the types of the operands, but I wouldn't recommend either way over the other.
If you understand the differences between strict and non-strict equality and you're confident that using ==
and !=
won't cause you or anyone else working your code any problems reading and understanding code in the future, go ahead and use them.
这篇关于远程JavaScript文件中的==与===。哪一个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!