resourceloader: Use null-objects for 'sources' and 'registry'
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 15 Sep 2018 17:54:19 +0000 (18:54 +0100)
committerKrinkle <krinklemail@gmail.com>
Sat, 15 Sep 2018 18:53:45 +0000 (18:53 +0000)
Follows-up e5912535aea, and removes the last use of
Object.prototype-inheriting objects for map-like objects, in
mediawiki.js.

I'd like to consider using ES6 Map as well, with a partial shim
like we do for StringSet/Set, but that'll require some more
refactoring. It's also not clear whether it will improve
execution speed and/or memory use. Worth trying at a later time,
though.

The current change simply removes the inheritance and simplifies
the code. Apart from slightly smaller code, I could not find any
notable/reproducible improvement in either NavTiming metrics or
CPU time spent in 'Scripting'. The metrics are in the same range
as before this changes.

Change-Id: Ie0016667d9291dcfafde61289d5444817be3447d

resources/src/startup/mediawiki.js

index b2aff43..bb04085 100644 (file)
@@ -13,7 +13,6 @@
        'use strict';
 
        var mw, StringSet, log,
-               hasOwn = Object.prototype.hasOwnProperty,
                trackQueue = [];
 
        /**
                         * @property
                         * @private
                         */
-                       var registry = {},
+                       var registry = Object.create( null ),
                                // Mapping of sources, keyed by source-id, values are strings.
                                //
                                // Format:
                                //         'sourceId': 'http://example.org/w/load.php'
                                //     }
                                //
-                               sources = {},
+                               sources = Object.create( null ),
 
                                // For queueModuleScript()
                                handlingPendingRequests = false,
                        function sortDependencies( module, resolved, unresolved ) {
                                var i, deps, skip;
 
-                               if ( !hasOwn.call( registry, module ) ) {
+                               if ( !( module in registry ) ) {
                                        throw new Error( 'Unknown dependency: ' + module );
                                }
 
                         *  or null if the module does not exist
                         */
                        function getModuleKey( module ) {
-                               return hasOwn.call( registry, module ) ?
-                                       ( module + '@' + registry[ module ].version ) : null;
+                               return module in registry ? ( module + '@' + registry[ module ].version ) : null;
                        }
 
                        /**
                         * @param {string} [skip]
                         */
                        function registerOne( module, version, dependencies, group, source, skip ) {
-                               if ( hasOwn.call( registry, module ) ) {
+                               if ( module in registry ) {
                                        throw new Error( 'module already registered: ' + module );
                                }
                                registry[ module ] = {
                                        // Appends a list of modules from the queue to the batch
                                        for ( q = 0; q < queue.length; q++ ) {
                                                // Only load modules which are registered
-                                               if ( hasOwn.call( registry, queue[ q ] ) && registry[ queue[ q ] ].state === 'registered' ) {
+                                               if ( queue[ q ] in registry && registry[ queue[ q ] ].state === 'registered' ) {
                                                        // Prevent duplicate entries
                                                        if ( batch.indexOf( queue[ q ] ) === -1 ) {
                                                                batch.push( queue[ q ] );
                                addSource: function ( ids ) {
                                        var id;
                                        for ( id in ids ) {
-                                               if ( hasOwn.call( sources, id ) ) {
+                                               if ( id in sources ) {
                                                        throw new Error( 'source already registered: ' + id );
                                                }
                                                sources[ id ] = ids[ id ];
                                                name = split.name,
                                                version = split.version;
                                        // Automatically register module
-                                       if ( !hasOwn.call( registry, name ) ) {
+                                       if ( !( name in registry ) ) {
                                                mw.loader.register( name );
                                        }
                                        // Check for duplicate implementation
                                        var module, state;
                                        for ( module in states ) {
                                                state = states[ module ];
-                                               if ( !hasOwn.call( registry, module ) ) {
+                                               if ( !( module in registry ) ) {
                                                        mw.loader.register( module );
                                                }
                                                setAndPropagate( module, state );
                                 *  in the registry.
                                 */
                                getVersion: function ( module ) {
-                                       return hasOwn.call( registry, module ) ? registry[ module ].version : null;
+                                       return module in registry ? registry[ module ].version : null;
                                },
 
                                /**
                                 *  in the registry.
                                 */
                                getState: function ( module ) {
-                                       return hasOwn.call( registry, module ) ? registry[ module ].state : null;
+                                       return module in registry ? registry[ module ].state : null;
                                },
 
                                /**