JavaScript - === vs ==运营商的表现 [英] JavaScript - === vs == operators performance

查看:204
本文介绍了JavaScript - === vs ==运营商的表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个星期前,我已经阅读了这个帖子 Is<比 C 中的比较运算符快于< =?。据说,< < = 之间的性能没有差异,因为它们被解释为相同/类似的机器命令。

A few weeks ago, I have read this thread Is < faster than <=? about comparison operators in C. It was said that there is no difference in the performance between < and <= as they are interpreted as same/similar machine commands.

同时,在我们公司的最佳实践中,据说我们应该总是用===来比较事物而不是==。所以,我开始怀疑这是否总是合适的,因为我习惯使用==和typeof ... ==并且不想改变我的写作方式: - ]

At the same time, in our company's "best practices", it was said that we should always use "===" to compare things instead of "==". So, I started to wonder if this is always appropriate as I am used to using the "==" and "typeof ... == " and do not want to change my way of writing :-]

请注意,这是在JavaScript的上下文中。

Note that this is in the context of JavaScript.

所以,我有一点研究,这里哪些等于运算符(== vs ===)应该用于JavaScript比较?据说:

So, I have a little research and here Which equals operator (== vs ===) should be used in JavaScript comparisons? it is said that:


这是因为等于运算符==确实类型强制...意味着
解释器隐式尝试转换值,然后
进行比较。

This is because the equality operator == does type coercion...meaning that the interpreter implicitly tries to convert the values and then does the comparing.

另一方面,身份运算符===不会输入
强制,因此在比较时不会转换价值
的值

On the other hand, the identity operator === does not do type coercion, and so thus it does not convert the values of the values when comparing

我开始怀疑如果这意味着当我使用===运算符时,我会变得很好性能,因为没有资源将用于转换操作数。在将所有代码转换为机器命令后,这是否意味着当您使用< C 没有区别$ c>和< = ,这在JavaScript和其他语言中是一样的吗?

And I started to wonder if this means that when I use the "===" operator, I will get good performance as no resources will be spent on converting the operands. And after all code is turned into machine commands, does this mean that just as there is no difference in C when you use < and <=, this is the same in JavaScript and other languages?

推荐答案

对于js,如果在字符串类型上使用,则===运算符将返回true,并且字符串是完全相同的字符。对于对象,它比较对象引用,而不是内容。

for js, the === operator will return true if used on string types and the strings are exactly the same characters. For objects it compares the object references, not the contents.

来自 ECMA标准


11.9.6严格等式比较算法比较x === y,其中x和y是值,产生真或假。这样的比较
执行如下:

11.9.6 The Strict Equality Comparison Algorithm The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:


  1. 如果Type(x)与Type(y)不同,则返回false。

  2. 如果Type(x)为Undefined,则返回true。

  3. 如果Type(x)为Null,则返回true。

  4. 如果Type(x)是Number,那么
    a。如果x是NaN,则返回false。
    b。如果y是NaN,则返回false。
    c。如果x与y的Number值相同,则返回true。
    d。如果x是+0且y是-0,则返回true。
    e。如果x是-0且y是+0,则返回true。
    f。返回false。

  5. 如果Type(x)是String,则如果x和y完全相同的字符序列(
    对应位置中的长度和字符相同),则返回true ;否则,返回false。

  6. 如果Type(x)是布尔值,如果x和y都为真或两者都为假,则返回true;

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then a. If x is NaN, return false. b. If y is NaN, return false. c. If x is the same Number value as y, return true. d. If x is +0 and y is -0, return true. e. If x is -0 and y is +0, return true. f. Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false;


这篇关于JavaScript - === vs ==运营商的表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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