Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed of comparing to null vs undefined in JavaScript

I have just run a very simple JavaScript performance test (don't ask why). 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:

var y = (x == null); and var y = (x == undefined);.

I was expecting the comparison with undefined to be the fasted. In fact it was nowhere near. The comparison with null was far and away the fastest, around 80% faster.

The results I've described above come from running the tests in Chrome (version 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).

So, my question is what could the cause of this be? Why does Chrome seem to favour the comparison with null so greatly?

For quick reference, here's a screenshot of the results:

enter image description here

like image 664
James Allardice Avatar asked Sep 02 '11 12:09

James Allardice


People also ask

Is it better to use undefined or null?

Only use null if you explicitly want to denote the value of a variable as having "no value". As @com2gz states: null is used to define something programmatically empty. undefined is meant to say that the reference is not existing. A null value has a defined reference to "nothing".

How do you compare null and undefined?

Comparing Equality of Null and Undefined Values Null and undefined values are equal when compared using the JavaScript equality operator. Use the equality operator (==) to compare if null and undefined values are equal in JavaScript.

What is the difference between null and undefined in JavaScript?

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value. You can assign it to a variable.

Why null == undefined is true in JavaScript?

null and undefined both return false . That's why your code is actually checking if false is equal to false . However their types are not equal. Because of that, the next statement will return false, as the === comparison operator checks both the types and their value.


2 Answers

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.

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.


If you need to actually check to see if something is undefined, you should use

if(typeof notSet == "undefined"){ } 

Proof

Try it... and set something to null in your JavaScript console.

null = "will error"; // Errors with --> ReferenceError: invalid assignment left-hand side 

However, if you try and do it with undefined, it won't error. That is not to say that you can override undefined, because you can't, but that undefined is its own primitive type.

The only real similarity between null and undefined, is that they can both be coerced into a boolean false.

like image 196
Layke Avatar answered Oct 01 '22 02:10

Layke


if i think well, they are not the same. so you can't use null instead of undefined.

typeof !== "undefined" vs. != null

like image 37
Gergely Fehérvári Avatar answered Oct 01 '22 03:10

Gergely Fehérvári