From e5912535aea0b10ceb6c1e5e41ad7aa19c779027 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 25 Aug 2018 11:57:25 +0200 Subject: [PATCH] resourceloader: Optimise several map-like objects with Object.create(null) Change-Id: Ia8a3159509536f58a97faadc2e72fb33f2d0e97f --- resources/src/mediawiki.base/mediawiki.base.js | 7 ++----- resources/src/startup/mediawiki.js | 14 +++++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/resources/src/mediawiki.base/mediawiki.base.js b/resources/src/mediawiki.base/mediawiki.base.js index 9536b678d9..284b21ad82 100644 --- a/resources/src/mediawiki.base/mediawiki.base.js +++ b/resources/src/mediawiki.base/mediawiki.base.js @@ -22,7 +22,6 @@ mwLoaderTrack = mw.track, trackCallbacks = $.Callbacks( 'memory' ), trackHandlers = [], - hasOwn = Object.prototype.hasOwnProperty, queue; /** @@ -401,7 +400,7 @@ * @class mw.hook */ mw.hook = ( function () { - var lists = {}; + var lists = Object.create( null ); /** * Create an instance of mw.hook. @@ -412,9 +411,7 @@ * @return {mw.hook} */ return function ( name ) { - var list = hasOwn.call( lists, name ) ? - lists[ name ] : - lists[ name ] = $.Callbacks( 'memory' ); + var list = lists[ name ] || ( lists[ name ] = $.Callbacks( 'memory' ) ); return { /** diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index f2168085f3..d41b7bd4a0 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -55,13 +55,13 @@ * @class */ function StringSet() { - this.set = {}; + this.set = Object.create( null ); } StringSet.prototype.add = function ( value ) { this.set[ value ] = true; }; StringSet.prototype.has = function ( value ) { - return hasOwn.call( this.set, value ); + return value in this.set; }; return StringSet; }() ); @@ -140,7 +140,7 @@ * copied in one direction only. Changes to globals do not reflect in the map. */ function Map( global ) { - this.values = {}; + this.values = Object.create( null ); if ( global === true ) { // Override #set to also set the global variable this.set = function ( selection, value ) { @@ -185,7 +185,7 @@ results = {}; for ( i = 0; i < selection.length; i++ ) { if ( typeof selection[ i ] === 'string' ) { - results[ selection[ i ] ] = hasOwn.call( this.values, selection[ i ] ) ? + results[ selection[ i ] ] = selection[ i ] in this.values ? this.values[ selection[ i ] ] : fallback; } @@ -194,7 +194,7 @@ } if ( typeof selection === 'string' ) { - return hasOwn.call( this.values, selection ) ? + return selection in this.values ? this.values[ selection ] : fallback; } @@ -247,13 +247,13 @@ var i; if ( Array.isArray( selection ) ) { for ( i = 0; i < selection.length; i++ ) { - if ( typeof selection[ i ] !== 'string' || !hasOwn.call( this.values, selection[ i ] ) ) { + if ( typeof selection[ i ] !== 'string' || !( selection[ i ] in this.values ) ) { return false; } } return true; } - return typeof selection === 'string' && hasOwn.call( this.values, selection ); + return typeof selection === 'string' && selection in this.values; } }; -- 2.20.1