typeof === 'undefined' verseseses

Benchmark created by Erichr on


Description

What is the perf diff for typeof val === 'undefined' vs val == null?

Preparation HTML

<div id="profiles"></div>
<div id="empties"></div>

Setup

// create 100 object items
    var csl = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$',
        cl = csl + '0123456789',
        n = 100,
        items = {},
        profilesReport = function ( items ) {
            var report = {},
                c = 0,
                k, t;
            for ( k in items )
                report[t = typeof items[k]]++ || ( report[t] = 1 );
            document.querySelector( '#profiles' ).appendChild( document.createTextNode( JSON.stringify( report ).replace( ',', "\n" ).slice( 1, -1 ) ) );
            document.querySelector( '#profiles' ).appendChild( document.createElement( 'br' ) )
        },
        emptiesReport = function ( items ) {
            var report = {},
                c = 0,
                k, t;
            for ( k in items )
                report[t = items[k] == null ? 'empty' : 'non-empty']++ || ( report[t] = 1 );
            document.querySelector( '#empties' ).appendChild( document.createTextNode( JSON.stringify( report ).replace( ',', "\n" ).slice( 1, -1 ) ) );
            document.querySelector( '#empties' ).appendChild( document.createElement( 'br' ) )
        },
        k, c;
    
    while ( n-- > 0 ) {
        c = Math.random() * 10 | 0;
        k = [ csl[Math.random() * csl.length | 0] ];
        while ( c > 0 )
            k[c--] = cl[Math.random() * cl.length | 0] 
        k = k.join( '' );
        switch( Math.random() * 5 | 0 ) {
            case 0: items[k] = null; break;
            case 1: items[k] = undefined; break;
            case 2: items[k] = NaN; break;
            case 3: items[k] = Math.random() < 0.5 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; break;
            case 4: items[k] = Math.random() < 0.5 ? true : false; break;
            case 5: items[k] = Math.random() * Number.MAX_VALUE | 0; break;
            case 6: items[k] = Math.random() * Number.MAX_VALUE; break;
            case 7: items[k] = /^regular expression$/i; break;
            case 8: items[k] = { objective: "dunno" }; break;
            case 9: items[k] = new Array( Math.random() * 100 | 0 ).join( '?' ); break;
            case 10: items[k] = new Array( Math.random() * 100 | 0 ).join( '?' ).split( '' ); break;
        }
    }
    
    profilesReport( items );
    emptiesReport( items );

Teardown


    items = null;
  

Test runner

Ready to run.

Testing in
TestOps/sec
equates to null
// for most purposes the time null and undefined mean the same thing, ie undefined
var empty = 0;
for ( k in items )
    items[k] == null && empty++;
ready
typeof exactly equals 'undefined'
// but then undefined and null are actually distinct and to give benefit to the doubt we won't check for both null ( === null, not typeof === 'object' ) and 'undefined'
var empty = 0;
for ( k in items )
    typeof items[k] === 'undefined' && empty++;
ready
exactly equals null or typeof exactly equals 'undefined'
// but then undefined and null are actually distinct and to be fair we check for both null ( === null, not typeof === 'object' ) and 'undefined'
var empty = 0, t;
for ( k in items )
    ( ( t = items[k] ) === null || typeof t === 'undefined' ) && empty++;
ready
typeof equals 'undefined'
// and finally the other specific way that doesn't detect null's
var empty = 0;
for ( k in items )
    typeof items[k] == 'undefined' && empty++;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Erichr on