38

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

James Allardice
  • 164,175
  • 21
  • 332
  • 312

4 Answers4

21

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.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Layke
  • 51,422
  • 11
  • 85
  • 111
  • That makes sense, but why then does Firefox not show similar results to Chrome? – James Allardice Sep 02 '11 at 12:54
  • I can't answer that James. I have no idea. Presumably it would be differences in the internals of their respective JS engines. The same way that Chrome is a lot faster for some heavy JavaScript sites, yet slower on some other parts. – Layke Sep 02 '11 at 13:04
  • 1
    @Layke what if he had used `===` instead of `==`? – Surender Thakran May 31 '13 at 05:01
  • `undefined` can be overridden within a function scope, if a function argument is named `undefined`. For example: `function foo(undefined) { console.log(undefined); }` – mwcz Dec 07 '15 at 20:12
1

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));    
BrokenBinary
  • 7,731
  • 3
  • 43
  • 54
twhitehead
  • 81
  • 2
  • 6
1

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

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

Community
  • 1
  • 1
Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74
1

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.

There are ways to check whether a variable is undefined. As the other posters have mentioned, typeof x === 'undefined' is one. (There's probably another possibility that is something like hasOwnProperty('x') executed on the global object, but that doesn't check the scope chain.)

Jason S
  • 184,598
  • 164
  • 608
  • 970