Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange code in jQuery sources: var !== var ? x : y;

Recently I found one weird line in the jQuery sources (last version 1.9.1, Sizzle package, line 129 funescape function):

funescape = function( _, escaped ) {
    var high = "0x" + escaped - 0x10000;
    // NaN means non-codepoint
    return high !== high ?            // <--- LINE 129
        escaped :
        // BMP codepoint
        high < 0 ?
            String.fromCharCode( high + 0x10000 ) :
            // Supplemental Plane codepoint (surrogate pair)
            String.fromCharCode( high >> 10 | 0xD800, high & 0x3FF | 0xDC00 );
};

What is the reason to make high !== high comparison? It looks like return escaped will never be executed. Or do I miss something?

Reference: jQuery Sizzle

like image 962
VisioN Avatar asked Feb 08 '13 11:02

VisioN


3 Answers

In fact it is written in the comment right above:

// NaN means non-codepoint

So it is mandatory to perform this comparison first to handle the NaN case as in JavaScript:

NaN === NaN returns false.

As pointed out by James Wiseman it is also important to know why the developer used high !== high instead of isNaN(high) which would have been clearer.

It is certainly based on performance. This test shows that a !== a is twenty times faster than isNaN(a).

zzzzBov also indicates that isNaN() could be overwritten, using !== is also more portable.

More info from Benjamin Gruenbaum:

It is also worth noting that NaN does not equal to anything else as well, and also it is not equal to anything else in an unstrict sense

And from Jan Dvorak:

Also note {valueOf:function(){return{}}} does equal itself

like image 152
koopajah Avatar answered Nov 17 '22 00:11

koopajah


The condition high !== high returns true, when high is NaN.I wonder why the jQuery guys did not used the much clear isNaN(high) function instead, but that was probably due to performance reasons as koopajah pointed out.

NaN (Not-a-Number) means a result that cannot be represented as a Number. It is an undeterminated number.


Why NaN === NaN returns false ?

Consider

0/0          = NaN
Math.asin(2) = NaN

You know that 0/0 is different than Math.asin(2), so why whould NaN be equal to NaN?

like image 23
Jose Rui Santos Avatar answered Nov 16 '22 22:11

Jose Rui Santos


I'm piggy-backing on some of the comments here, but think this worthy information.

Some comments on the original question have suggested that this method of checking for NaN is actually much faster than isNaN()

When taken in conjunction with the following alternative to parseInt parseFloat we have a very fast way of converting to a number and checking its numeric state.

Is Subtracting Zero some sort of JavaScript performance trick?

So instead of

function Translated(val) {
    var x = parseFloat(val);
    if (!isNaN(x)) {
        alert("Not a number");
    }
}

We can have

function WTF(val) {
    var x = val - 0;
    if (x !== x) {
        alert("Not a number");
    }
}
like image 6
James Wiseman Avatar answered Nov 16 '22 22:11

James Wiseman