问题
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
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?
回答1:
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.
回答2:
Presumably blogs that recommend one over the other are doing so for reasons of logical consistency with analogous operations and not for speed. Trying to trim your Javascript programs to point of shaving off individual characters from the script is unwise; running it through minify or another automated tool before serving is one thing, but hand-tuning Javascript for minimum file size or for execution speed on the level of individual operands is an endless, thankless task that will make your site harder to maintain.
Use whichever operand makes more logical sense to you, so you won't be confused when you don't remember this line of inquiry two years from now.
回答3:
If I may quote from Douglas Crockford's Javascript: The Good Parts:
JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. The rules by which they do that are complicated and unmemorable ... The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==.
Whether there is a performance difference or not, it would be difficult to justify the use of the "evil twins".
回答4:
var test="1";
console.log(test==1); // true
console.log(test===1); // false
==
checks whether test has 1 or not but ===
checks whether test has 1 and also checks it's data type. In that case 2nd expression is false because it's data type is string (test is string) but the right hand operand is not string. The following test is different
var test=1;
console.log(test==1); // true
console.log(test===1); // true
Because test contains Integer
1 that evaluates to Boolean
true and right hand operand is also same. ===
also checks whether both operands type.
来源:https://stackoverflow.com/questions/10259774/vs-in-a-remote-javascript-file-which-one-is-faster