jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
What is the perf diff for typeof val === 'undefined' vs val == null?
<div id="profiles"></div>
<div id="empties"></div>
// 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 );
items = null;
Ready to run.
Test | Ops/sec | |
---|---|---|
equates to null |
| ready |
typeof exactly equals 'undefined' |
| ready |
exactly equals null or typeof exactly equals 'undefined' |
| ready |
typeof equals 'undefined' |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.