Profiling `if (x)` VS `if (x === undefined)`

<script>
    function f(){
        var t=document.getElementById("t");
        var g=t.asdfg;
        var a=new Date().getTime();
        for(var x=0;x<100000000;++x){
            if(g===undefined);
            //if(g);
        }
        var b=new Date().getTime();
        alert(b-a);
    }
</script>
<body onload="f();">
    <input id="t"/>
</body>

Firefox is if(g)slower than if(g===undefined). I am wondering if anyone knows a good explanation why this is so?

in Chrome is if(g)faster than if(g===undefined). I am wondering if anyone knows a good explanation why this is so?

I am not saying that we should use one over the other. I'm interested in theory

Perhaps different browsers have different implementations, but any theory in any browser would be worthy of an answer

+5
source share
1 answer

There is no keyword in javascript undefined, it does not work as nullit does.

When you use

if(g===undefined)

undefined . undefined () . .

IMHO, ( )

if(typeof(g) === 'undefined')

, if(g), , , , , .

, Firefox Chrome.

+6

All Articles