标签云

微信群

扫码加入我们

WeChat QR Code


To whom it might be interested in the same subject === vs ==, but in PHP, can read here: stackoverflow.com/questions/2401478/why-is-faster-than-in-php/…

2018年11月18日17分50秒

Just in case anyone was wondering in 2012: === is way faster than ==. jsperf.com/comparison-of-comparisons

2018年11月18日17分50秒

minitech it should be as it does not do type conversion

2018年11月17日17分50秒

minitech, I doubt anyone is going to make their application noticeably faster by using === over ==. In fact, the benchmark doesn't show a big difference between both on modern browsers. Personally, I usually use == everywhere unless I really need strict equality.

2018年11月17日17分50秒

Here is the part of Crockford's JS The Good Parts talk where he discusses the === and == operators: youtube.com/… If it doesn't play, it's at 15:20

2018年11月17日17分50秒

=== is not quicker if the types are the same. If types are not the same, === will be quicker because it won't try to do the conversion.

2018年11月17日17分50秒

=== will never be slower than ==. They both do type checking, so === doesn't do anything extra compared to ==, but the type check may allow === to exit sooner when types are not the same.

2018年11月17日17分50秒

Replacing all ==/!= with ===/!== increases the size of the js file, it will take then more time to load. :)

2018年11月17日17分50秒

"...the rules by which they do that are complicated and unmemorable..." Now such statements make you feel so safe when programming...

2018年11月18日17分50秒

Sometimes JavaScript's type system makes me want to run away screaming.

2018年11月17日17分50秒

Software Monkey: not for value types (number, boolean, ...)

2018年11月18日17分50秒

type coercion vs type casting vs type convertion: stackoverflow.com/questions/8857763/…

2018年11月17日17分50秒

Since nobody has mentioned the Javascript Equality Table, here it is: dorey.github.io/JavaScript-Equality-Table

2018年11月17日17分50秒

In the first statement, are you sure that 'true' is converted to 1 and not 1 converted to true?

2018年11月18日17分50秒

Where do the terms "equality" and "identity" come from? The standard does not use those terms. It calls == "abstract equality" and it calls === "strict equality". Granted calling == any kind of "equality" is IMHO awful, since it is not transitive, but why quibble? I take more issue with "identity" though; I think that term is pretty misleading, though it "works." But seriously, who coined the term "identity"? I search the standard and could not find it.

2018年11月18日17分50秒

activa: I would clarify, that the strings are so equal only when they are literals. new String("abc") === "abc" is false (according to my research).

2018年11月17日17分50秒

new Number() == "0". Also in Firefox: (function(){}) == "function () {\n}"

2018年11月17日17分50秒

Thank you for explaining why new String("123") !== "123". They are different types. Simple, yet confusing.

2018年11月18日17分50秒

String objects behave as strings as does any other object. new String should never be used, as that doesn't create real strings. A real string and can be made with string literals or calling String as a function without new, for example: String(0); //"0", Real string, not an object

2018年11月17日17分50秒

But in the cases you detailed, the operator "==" behaves exactly the same.

2018年11月17日17分50秒

I would go even further with the moral: always use ===/!=== but understand the conversion for being able to cope with other people's code

2018年11月18日17分50秒

the running implementation of this table is here: yolpo.com/embed.html?gist=344311f27fd88a9c2be8

2018年11月17日17分50秒

mfeineis you mean === or !== instead of == or != . Don't want to confuse new coders ;)

2018年11月17日17分50秒

from my experience using three equals can cause problems and should be avoided unless fully understood. two equals produces much better results because 99% of the time I really don't want types to be equal.

2018年11月17日17分50秒

vsync: If you really don't want types to be equal, you should use three equals!

2018年11月18日17分50秒

Result of Type(x) is implied to be the same as typeof ?

2018年11月18日17分50秒

nalply I don't exactly understand the anxiety about the behavior with new String('x'), because I've never seen any code in the wild that uses primitive wrapper objects, and I don't think there's much good reason to, especially not these days. Have you ever encountered code that does?

2018年11月18日17分50秒

Andy the problem is malicious or just sloppy third-party code, then you can't assume that nobody uses new String().

2018年11月18日17分50秒

If it's sloppy, === is how you will find out. If it's malicious, I think new String() is probably the least of your worries. I understand the concern in theory, but again, do you have any real-world examples? To me it's like the old anxiety that someone could set undefined to another value.

2018年11月17日17分50秒

David: correct. That's why this answer is inaccurate (or even wrong)

2018年11月17日17分50秒

David var a = {}, b = {}; a == b returns false.

2018年11月17日17分50秒

Yes: Two different objects with the same type and value compare false, i.e., this answer is just wrong. Why does it have 50 upvotes?

2018年11月17日17分50秒

I realize this is old, but to clarify why this answer is still "correct" is because in the example var a = {}, b = {}; While both a and b is indeed both an object, but they are not the same value, technically speaking. They are different instances. Note that comparing instances behaves differently than comparing primitives. Which probably adds to this confusion. You will see similar comparison behavior if you use instance version of primitive data types. E.g new String('asdf') or new Number(5). Ex: new Number(5) == new Number(5) is false, even though they hold the same value.

2018年11月17日17分50秒

We all forget that a reference to an object is actually a Value Type, as it a pointer to a memory slot. The Object comparison is not comparing the "value of the object" but whether both pointers are the same which would mean they reference the same memory slot. That is a very subtle difference in comparing Types, as the "===" operator really needs to say "if type, value, and reference to the object in memory are the same".

2018年11月18日17分50秒

More than type safety you want logical correctness - sometimes you want things to be truthy when == disagrees.

2018年11月17日17分50秒

Now, how do these compare when there is an actual type coersion for == operator? Remember, that's when there's a performance boost.

2018年11月17日17分50秒

MAJOR difference when tested properly for the aforementioned reasons of quicker to only check type inequality. jsfiddle.net/4jhuxkb2

2018年11月18日17分50秒

Here is a detailed article on comparison of === vs ==

2018年11月17日17分50秒

also, 'string' !== 'number'

2018年11月18日17分50秒

Your looseEqual is wrong. Function == Function.toString() is true, but looseEqual(Function, Function.toString()) is false. Not sure why you filter out functions at the beginning.

2018年11月18日17分50秒

Oriol you were right, I updated the code to account for that, FYI based on my tests it wasn't enough to remove the filter for "functions", instead "functions" had to be handled differently altogether.

2018年11月18日17分50秒

Be aware the spec doesn't treat functions differently, they are just objects. The problem seems that you rely on typeof x === "object" to check if it's an object, but `typeof only works for non-null primitives. You might be interested in my list of proper ways to check if a value is an object

2018年11月18日17分50秒

You're right, great observations, this emphasizes the main point that == does a lot of things making it very difficult to anticipate the results while === is much more straightforward and predictable which is one of the main reasons === is the recommended choice. (I'll add a note to the answer mentioning your point)

2018年11月17日17分50秒

best explanation on the topic I could find, thx

2018年11月18日17分50秒

I don't understand why the string arrow is pointing to the big gray box, is it supposed to mean the interrupter is casting the string to a number?

2018年11月18日17分50秒

vsync It points to the string option within the grey box i.e string -> # || NaN. Javascript is not a type-script language i.e basically it can have any type of variable. So, it is pointed to that grey box.

2018年11月18日17分50秒

I simply asked if it is for casting purposes since the string is supposed to be compared to a type number, so the interrupter looks at what the string should be compared to and cast the string accordingly?

2018年11月17日17分50秒

The big gray box is what ToNumber would return when given different types, so if it is given a string it will only choose the last option (and convert it to a number). == uses ToNumber only in the cases string == number or boolean == anything above (and only on the string/boolean). This means == will never convert undefined or null even though they are in the gray box. (For any combination of either undefined or null or both, == will always return true. Also, whether a value is on the left or right side doesn't matter, == (and ===) will return the same result.)

2018年11月17日17分50秒

Not always true. With gzip compression, the difference would be almost negligible.

2018年11月17日17分50秒

Agree, but thousand "===" means also 10 thousands of code lines else so 1kb more or less... ;)

2018年11月17日17分50秒

f your that concerned about size then just swap all your == with ===, then use a regexp wrapped in av eval to switch it back

2018年11月18日17分50秒

Here is a detailed article on comparison of === vs ==

2018年11月17日17分50秒

In JavaScript, this is completely wrong and wrongly incomplete. 0 != null. -1

2018年11月17日17分50秒

0 == null is false.

2018年11月17日17分50秒

ADS