performance - Speed of comparing to null vs undefined in JavaScript -


I have just run a very simple JavaScript (why not ask) test declares a variable, but nothing is assigned to it Does:

var x;

This again compares the comparison of the variable variable null , and undefined , in other words:

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

I was expecting to compare with undefined for fasting. Actually it was somewhere. The comparison with null was fastest, about 80% faster.

The results described above come from running tests in Chrome (version 13). Running them in Firefox produces too many results (compared to undefined in comparison to null , but it is very minor).

So, my question is, what could be the reason? Why does Chrome look too much to compare with null ?

For quick reference, here is a screenshot of the results:

 Enter an image description

null is a reserved keyword that overrides So, when you're comparing it with Null, you only have to make a comparison

However, when you are examining against undefined So the engine should do a type of lookup and then one Lana, which means that this is actually a bit more demanding.


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

  (if not set == "undefined" ) {}   

proof

Try it ... and set something on null in your Javascript console.

  cord = "error"; // errors with - & gt; Reference error: Invalid assignment on the left   

However, if you try and undefine it, then this error will not occur. It is not that you can override undefined , because you can not do it, but this is undefined is its own old type.

The similarity between the only real empty and undefined is that both of them can be included in boolean false.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -