=== vs == operators performance

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.

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 :-]

Note that this is in the context of 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

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?


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.

From the ECMA standard:

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:

  • If Type(x) is different from Type(y), return false.
  • If Type(x) is Undefined, return true.
  • If Type(x) is Null, return true.
  • 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.
  • 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.
  • If Type(x) is Boolean, return true if x and y are both true or both false;

  • Firstly, performance simply is not a concern. For any real script, any performance gain of using one operator over the other will be infinitessimally small compared to other bottlenecks in the code (typically DOM manipulation would be the number one target).

    Secondly, in many cases, == and === will perform exactly the same steps . When the types of the two operands are the same (two strings or two numbers, for example), the ECMAScript specification has precisely the same steps for the two operators. Therefore if you observe a performance difference between the two operators for operands of the same type in one browser or other environment, there is no guarantee or even likelihood that you will see a similar difference in another browser.

    In the case of typeof , as mentioned in your question, the two operands are guaranteed to be of the same type (string) and both operators will do precisely the same thing, so the only reasons to favour one operator over the other are stylistic .

    The JS community as a whole has gone rather hardline on this: the consensus seems to be "never use == and != unless you need type coercion", which is too dogmatic for my tastes.


    I feel an answer with easily verifiable evidence would be best.

    These operations are so small that it is difficult to performance test them.

  • == 1648 true
  • === 1629 true
  • control test 1575 true
  • If you subtract off the control test, it looks like there is a ~30% difference in their speeds on my browser. If you do this multiple times, you can get different answers, but === usually comes up the fastest, which I think is just a testament to just how negligible the difference is.

    I think this pretty much proves what others were saying, that the performance difference is a waste of time to think about, but it also shows that === is actually faster. Hopefully this answer can save other people time, those who simply must see proof.

    var testString = "42";
    var testNumber = 42;
    var testObject = {};
    
    
    var start = Date.now();
    var result = null;
    for(var i = 0; i < 100000000; i++){
        result = testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject && 
        testString == testString && testNumber == testNumber && testObject == testObject
    }
    
    console.log("==", Date.now() - start, result);
    
    var start = Date.now();
    var result = null;
    for(var i = 0; i < 100000000; i++){
        result = testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject && 
        testString === testString && testNumber === testNumber && testObject === testObject
    }
    console.log("===", Date.now() - start, result);
    var start = Date.now();
    for(var i = 0; i < 100000000; i++){
        result =  true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true && 
        true && true && true
    }
    console.log("control test", Date.now() - start, result);
    链接地址: http://www.djcxy.com/p/31564.html

    上一篇: 哪一个更高性能:<= 0或<1?

    下一篇: === vs ==运营商的表现