[英]Speed of comparing to null vs undefined in JavaScript
I have just run a very simple JavaScript performance test (don't ask why). 我刚刚运行了一个非常简单的JavaScript 性能测试 (不要问为什么)。 The test declares a variable, but doesn't assign anything to it: 测试声明了一个变量,但没有为其赋值:
var x;
It then compares the speed of comparing the value variable to null
, and to undefined
, in other words: 然后它将比较value变量的速度与null
和undefined
,换句话说:
var y = (x == null);
and var y = (x == undefined);
和var y = (x == undefined);
. 。
I was expecting the comparison with undefined
to be the fasted. 我期待与undefined
的比较是禁食。 In fact it was nowhere near. 事实上,它远远不够。 The comparison with null
was far and away the fastest, around 80% faster. 与null
的比较是最快的,大约快80%。
The results I've described above come from running the tests in Chrome (version 13). 我上面描述的结果来自于在Chrome中运行测试(版本13)。 Running them in Firefox produces results far closer to what I would have expected (the comparison with undefined
is faster than with null
, albeit very marginally). 在Firefox中运行它们会产生更接近我预期的结果(与undefined
的比较比使用null
更快,尽管非常小)。
So, my question is what could the cause of this be? 所以,我的问题是这可能是什么原因? Why does Chrome seem to favour the comparison with null
so greatly? 为什么Chrome似乎更喜欢与null
的比较呢?
For quick reference, here's a screenshot of the results: 为了快速参考,这里是结果的屏幕截图:
null
is a reserved keyword which cannot be overriden, so when you are doing a comparison against null, all you have to do is a single comparison. null
是一个无法覆盖的保留关键字,因此当您对null进行比较时,您所要做的只是一次比较。
However, when you are checking against undefined
, the engine must do a type lookup and then a comparison, meaning that it is actually slightly more demanding. 但是,当您检查undefined
,引擎必须进行类型查找然后进行比较,这意味着它实际上要求更高。
If you need to actually check to see if something is undefined, you should use 如果您需要实际检查以确定某些内容是否未定义,您应该使用
if(typeof notSet == "undefined"){ }
Try it... and set something to null
in your JavaScript console. 试试吧......并在JavaScript控制台中将某些内容设置为null
。
null = "will error";
// Errors with --> ReferenceError: invalid assignment left-hand side
However, if you try and do it with undefined, it won't error. 但是,如果您尝试使用undefined进行操作,则不会出错。 That is not to say that you can override undefined
, because you can't, but that undefined
is its own primitive type. 这并不是说你可以覆盖undefined
,因为你不能,但undefined
是它自己的原始类型。
The only real similarity between null and undefined, is that they can both be coerced into a boolean false. null和undefined之间唯一真正的相似之处在于它们都可以被强制转换为布尔值false。
I recently discovered that this: 我最近发现这个:
if (typeof this._minLat === 'undefined') {
this._minLat = Math.min(...this.points.map(point => point.lat));
}
return this._minLat;
seems to be many times faster than this: 似乎比这快很多倍:
return this._minLat || Math.min(...this.points.map(point => point.lat));
if i think well, they are not the same. 如果我认为好,他们就不一样了。 so you can't use null instead of undefined. 所以你不能使用null而不是undefined。
typeof !== "undefined" vs. != null typeof!==“undefined”vs.!= null
You're comparing against the lookup of a variable called undefined
(which returns an undefined value), so it's not doing what you were intending. 您正在与名为undefined
(返回未定义的值)的变量的查找进行比较,因此它不会执行您想要的操作。
There are ways to check whether a variable is undefined. 有一些方法可以检查变量是否未定义。 As the other posters have mentioned, typeof x === 'undefined'
is one. 正如其他海报所提到的, typeof x === 'undefined'
就是一个。 (There's probably another possibility that is something like hasOwnProperty('x')
executed on the global object, but that doesn't check the scope chain.) (可能还有另一种可能就是在全局对象上执行hasOwnProperty('x')
,但这并不会检查作用域链。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.