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: