mw.loader: Use Object.create() instead of $.extend() where possible
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 14 Sep 2017 17:41:36 +0000 (19:41 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 18 Sep 2017 17:29:21 +0000 (18:29 +0100)
For cases where we want a variation of an object with some keys added
or changed, use native Object.create() to create a plain object that
inherits from the other object (without needing to loop and copy each
key/value separately).

This optimisation only works, of course, if the original object
by-reference does not change later.

Also remove hasOwn filter from sortQuery, because it must now consider
inherited keys as well. This filter was already obsolete given mw.loader
already has many unfiltered for-in loops in other places, and just in
general doesn't support environments that extend Object.prototype.

Change-Id: Ib405e32d4598dfb94629c2596ea0804d1c13e7c0

resources/src/mediawiki/mediawiki.js

index 0ae45b5..b522486 100644 (file)
                                        a = [];
 
                                for ( key in o ) {
-                                       if ( hasOwn.call( o, key ) ) {
-                                               a.push( key );
-                                       }
+                                       a.push( key );
                                }
                                a.sort();
                                for ( key = 0; key < a.length; key++ ) {
                         * @param {string} sourceLoadScript URL of load.php
                         */
                        function doRequest( moduleMap, currReqBase, sourceLoadScript ) {
-                               var query = $.extend(
-                                       { modules: buildModulesString( moduleMap ) },
-                                       currReqBase
-                               );
+                               // Optimisation: Inherit (Object.create), not copy ($.extend)
+                               var query = Object.create( currReqBase );
+                               query.modules = buildModulesString( moduleMap );
                                query = sortQuery( query );
                                addScript( sourceLoadScript + '?' + $.param( query ) );
                        }
                                                // modules for this group from this source.
                                                modules = splits[ source ][ group ];
 
-                                               currReqBase = $.extend( {
-                                                       version: getCombinedVersion( modules )
-                                               }, reqBase );
+                                               // Optimisation: Inherit (Object.create), not copy ($.extend)
+                                               currReqBase = Object.create( reqBase );
+                                               currReqBase.version = getCombinedVersion( modules );
+
                                                // For user modules append a user name to the query string.
                                                if ( group === 'user' && mw.config.get( 'wgUserName' ) !== null ) {
                                                        currReqBase.user = mw.config.get( 'wgUserName' );