Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Last active September 7, 2016 20:27
Show Gist options
  • Save Rich-Harris/963b0bb31151fa1039a6e427c16061a9 to your computer and use it in GitHub Desktop.
Save Rich-Harris/963b0bb31151fa1039a6e427c16061a9 to your computer and use it in GitHub Desktop.
Demonstrating that `~arr.indexOf(key)` is actually faster than `lookup[key]`
// paste me into the console
var ALLOWED_KEYS = [
'acorn',
'banner',
'cache',
'context',
'dest',
'entry',
'exports',
'external',
'footer',
'format',
'globals',
'indent',
'intro',
'moduleId',
'moduleName',
'noConflict',
'onwarn',
'outro',
'paths',
'plugins',
'preferConst',
'sourceMap',
'sourceMapFile',
'targets',
'treeshake',
'useStrict'
];
function validateKeysWithArray ( object, allowedKeys ) {
var actualKeys = keys( object );
var i = actualKeys.length;
while ( i-- ) {
var key = actualKeys[i];
if ( allowedKeys.indexOf( key ) === -1 ) {
return new Error(
`Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }`
);
}
}
}
function arrayToMap(list) {
return list.reduce((accum, el) => {
accum[el] = el
return accum
}, {})
}
function validateKeysWithMap ( object, allowedKeys ) {
var actualKeys = keys( object );
var keysMap = arrayToMap(allowedKeys);
var i = actualKeys.length;
while ( i-- ) {
var key = actualKeys[i];
if ( !keysMap[key] ) {
return new Error(
`Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }`
);
}
}
}
console.time( 'validateKeysWithArray' );
validateKeysWithArray({
entry: 'src/index.js',
dest: 'bundle.js',
format: 'cjs'
}, ALLOWED_KEYS );
console.timeEnd( 'validateKeysWithArray' );
console.time( 'validateKeysWithMap' );
validateKeysWithMap({
entry: 'src/index.js',
dest: 'bundle.js',
format: 'cjs'
}, ALLOWED_KEYS );
console.timeEnd( 'validateKeysWithMap' );
console.time( 'validateKeysWithArray 10000 times' );
var i = 10000;
while ( i-- ) {
validateKeysWithArray({
entry: 'src/index.js',
dest: 'bundle.js',
format: 'cjs'
}, ALLOWED_KEYS );
}
console.timeEnd( 'validateKeysWithArray 10000 times' );
console.time( 'validateKeysWithMap 10000 times' );
var i = 10000;
while ( i-- ) {
validateKeysWithMap({
entry: 'src/index.js',
dest: 'bundle.js',
format: 'cjs'
}, ALLOWED_KEYS );
}
console.timeEnd( 'validateKeysWithMap 10000 times' );
function validateKeysWithCachedMap ( object, allowedKeys, keysMap ) {
var actualKeys = keys( object );
var i = actualKeys.length;
while ( i-- ) {
var key = actualKeys[i];
if ( !keysMap[key] ) {
return new Error(
`Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }`
);
}
}
}
console.time( 'validateKeysWithCachedMap 10000 times' );
var cachedMap = arrayToMap(ALLOWED_KEYS);
var i = 10000;
while ( i-- ) {
validateKeysWithCachedMap({
entry: 'src/index.js',
dest: 'bundle.js',
format: 'cjs'
}, ALLOWED_KEYS, cachedMap );
}
console.timeEnd( 'validateKeysWithCachedMap 10000 times' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment