From 11e81ef63b26955aeb6dbb1d6ef01bb0c0aa7e85 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 24 Aug 2018 02:58:56 +0100 Subject: [PATCH] resourceloader: Optimise several map-like objects with Object.create(null) Object.create(null) is specifically for the use case of being able to store any key/value without worrying about inherited built-in methods. As such, using Object.create(null) in these cases has two benefits: * Removes the need for a hasOwn() call. One can instead use the 'in' operator, or look for undefined, without there being gotchas due to inherited methods. * When the known values are always truthy (e.g. when only storing value that are also objects or arrays), there is no need for any explicit comparison (plain boolean evaluation). Change-Id: I24f86f9938e4f5ccca81d15aa14a48ac3ddee341 --- resources/src/startup/mediawiki.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index 6ed226cf38..adde3ee07f 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -1450,7 +1450,7 @@ * @param {string[]} batch */ function batchRequest( batch ) { - var reqBase, splits, maxQueryLength, b, bSource, bGroup, bSourceGroup, + var reqBase, splits, maxQueryLength, b, bSource, bGroup, source, group, i, modules, sourceLoadScript, currReqBase, currReqBaseLength, moduleMap, currReqModules, l, lastDotIndex, prefix, suffix, bytesAdded; @@ -1492,22 +1492,20 @@ maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', 2000 ); // Split module list by source and by group. - splits = {}; + splits = Object.create( null ); for ( b = 0; b < batch.length; b++ ) { bSource = registry[ batch[ b ] ].source; bGroup = registry[ batch[ b ] ].group; - if ( !hasOwn.call( splits, bSource ) ) { - splits[ bSource ] = {}; + if ( !splits[ bSource ] ) { + splits[ bSource ] = Object.create( null ); } - if ( !hasOwn.call( splits[ bSource ], bGroup ) ) { + if ( !splits[ bSource ][ bGroup ] ) { splits[ bSource ][ bGroup ] = []; } - bSourceGroup = splits[ bSource ][ bGroup ]; - bSourceGroup.push( batch[ b ] ); + splits[ bSource ][ bGroup ].push( batch[ b ] ); } for ( source in splits ) { - sourceLoadScript = sources[ source ]; for ( group in splits[ source ] ) { @@ -1533,7 +1531,7 @@ // We may need to split up the request to honor the query string length limit, // so build it piece by piece. l = currReqBaseLength; - moduleMap = {}; // { prefix: [ suffixes ] } + moduleMap = Object.create( null ); // { prefix: [ suffixes ] } currReqModules = []; for ( i = 0; i < modules.length; i++ ) { @@ -1542,7 +1540,7 @@ // If lastDotIndex is -1, substr() returns an empty string prefix = modules[ i ].substr( 0, lastDotIndex ); suffix = modules[ i ].slice( lastDotIndex + 1 ); - bytesAdded = hasOwn.call( moduleMap, prefix ) ? + bytesAdded = moduleMap[ prefix ] ? suffix.length + 3 : // '%2C'.length == 3 modules[ i ].length + 3; // '%7C'.length == 3 @@ -1552,12 +1550,12 @@ doRequest(); // .. and start again. l = currReqBaseLength; - moduleMap = {}; + moduleMap = Object.create( null ); currReqModules = []; mw.track( 'resourceloader.splitRequest', { maxQueryLength: maxQueryLength } ); } - if ( !hasOwn.call( moduleMap, prefix ) ) { + if ( !moduleMap[ prefix ] ) { moduleMap[ prefix ] = []; } l += bytesAdded; -- 2.20.1