resourceloader: Optimise several map-like objects with Object.create(null)
authorFomafix <fomafix@googlemail.com>
Sat, 25 Aug 2018 09:57:25 +0000 (11:57 +0200)
committerKrinkle <krinklemail@gmail.com>
Tue, 28 Aug 2018 00:25:58 +0000 (00:25 +0000)
Change-Id: Ia8a3159509536f58a97faadc2e72fb33f2d0e97f

resources/src/mediawiki.base/mediawiki.base.js
resources/src/startup/mediawiki.js

index 9536b67..284b21a 100644 (file)
@@ -22,7 +22,6 @@
                mwLoaderTrack = mw.track,
                trackCallbacks = $.Callbacks( 'memory' ),
                trackHandlers = [],
-               hasOwn = Object.prototype.hasOwnProperty,
                queue;
 
        /**
         * @class mw.hook
         */
        mw.hook = ( function () {
-               var lists = {};
+               var lists = Object.create( null );
 
                /**
                 * Create an instance of mw.hook.
                 * @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 {
                                /**
index f216808..d41b7bd 100644 (file)
                         * @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;
                }() );
         *  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 ) {
                                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;
                                        }
                        }
 
                        if ( typeof selection === 'string' ) {
-                               return hasOwn.call( this.values, selection ) ?
+                               return selection in this.values ?
                                        this.values[ selection ] :
                                        fallback;
                        }
                        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;
                }
        };