r91386 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91385‎ | r91386 | r91387 >
Date:23:13, 3 July 2011
Author:krinkle
Status:ok
Tags:
Comment:
Minor optimalizations and jsperfs in mediawiki.js
* Use strict comparison to undefined where possible (arguments and object/array members)
* Make sure $ is not implied global (using jQuery.isFunction at the bottom instead of creating another 1-line closure)
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -52,7 +52,7 @@
5353 return results;
5454 } else if ( typeof selection === 'string' ) {
5555 if ( this.values[selection] === undefined ) {
56 - if ( typeof fallback !== 'undefined' ) {
 56+ if ( fallback !== undefined ) {
5757 return fallback;
5858 }
5959 return null;
@@ -75,7 +75,7 @@
7676 this.values[s] = selection[s];
7777 }
7878 return true;
79 - } else if ( typeof selection === 'string' && typeof value !== 'undefined' ) {
 79+ } else if ( typeof selection === 'string' && value !== undefined ) {
8080 this.values[selection] = value;
8181 return true;
8282 }
@@ -116,7 +116,7 @@
117117 this.format = 'parse';
118118 this.map = map;
119119 this.key = key;
120 - this.parameters = typeof parameters === 'undefined' ? [] : $.makeArray( parameters );
 120+ this.parameters = parameters === undefined ? [] : $.makeArray( parameters );
121121 return this;
122122 }
123123
@@ -367,7 +367,7 @@
368368 * Recursively resolves dependencies and detects circular references
369369 */
370370 function recurse( module, resolved, unresolved ) {
371 - if ( typeof registry[module] === 'undefined' ) {
 371+ if ( registry[module] === undefined ) {
372372 throw new Error( 'Unknown dependency: ' + module );
373373 }
374374 // Resolves dynamic loader function and replaces it with its own results
@@ -441,7 +441,7 @@
442442 }
443443 // If called without a list of modules, build and use a list of all modules
444444 var list = [], module;
445 - if ( typeof modules === 'undefined' ) {
 445+ if ( modules === undefined ) {
446446 modules = [];
447447 for ( module in registry ) {
448448 modules[modules.length] = module;
@@ -450,7 +450,7 @@
451451 // Build a list of modules which are in one of the specified states
452452 for ( var s = 0; s < states.length; s++ ) {
453453 for ( var m = 0; m < modules.length; m++ ) {
454 - if ( typeof registry[modules[m]] === 'undefined' ) {
 454+ if ( registry[modules[m]] === undefined ) {
455455 // Module does not exist
456456 if ( states[s] == 'undefined' ) {
457457 // OK, undefined
@@ -475,7 +475,7 @@
476476 */
477477 function execute( module, callback ) {
478478 var _fn = 'mw.loader::execute> ';
479 - if ( typeof registry[module] === 'undefined' ) {
 479+ if ( registry[module] === undefined ) {
480480 throw new Error( 'Module has not been registered yet: ' + module );
481481 } else if ( registry[module].state === 'registered' ) {
482482 throw new Error( 'Module has not been requested from the server yet: ' + module );
@@ -837,7 +837,7 @@
838838 if ( typeof module !== 'string' ) {
839839 throw new Error( 'module must be a string, not a ' + typeof module );
840840 }
841 - if ( typeof registry[module] !== 'undefined' ) {
 841+ if ( registry[module] !== undefined ) {
842842 throw new Error( 'module already implemented: ' + module );
843843 }
844844 // List the module as registered
@@ -845,7 +845,7 @@
846846 'state': 'registered',
847847 'group': typeof group === 'string' ? group : null,
848848 'dependencies': [],
849 - 'version': typeof version !== 'undefined' ? parseInt( version, 10 ) : 0
 849+ 'version': version !== undefined ? parseInt( version, 10 ) : 0
850850 };
851851 if ( typeof dependencies === 'string' ) {
852852 // Allow dependencies to be given as a single module name
@@ -886,13 +886,11 @@
887887 throw new Error( 'msgs must be an object, not a ' + typeof msgs );
888888 }
889889 // Automatically register module
890 - if ( typeof registry[module] === 'undefined' ) {
 890+ if ( registry[module] === undefined ) {
891891 mw.loader.register( module );
892892 }
893893 // Check for duplicate implementation
894 - if ( typeof registry[module] !== 'undefined'
895 - && typeof registry[module].script !== 'undefined' )
896 - {
 894+ if ( registry[module] !== undefined && registry[module].script !== undefined ) {
897895 throw new Error( 'module already implemeneted: ' + module );
898896 }
899897 // Mark module as loaded
@@ -978,7 +976,7 @@
979977 href: modules
980978 } ) );
981979 return true;
982 - } else if ( type === 'text/javascript' || typeof type === 'undefined' ) {
 980+ } else if ( type === 'text/javascript' || type === undefined ) {
983981 addScript( modules );
984982 return true;
985983 }
@@ -1122,7 +1120,7 @@
11231121 for ( var attrName in attrs ) {
11241122 s += ' ' + attrName + '="' + this.escape( attrs[attrName] ) + '"';
11251123 }
1126 - if ( typeof contents == 'undefined' || contents === null ) {
 1124+ if ( contents === undefined || contents === null ) {
11271125 // Self close tag
11281126 s += '/>';
11291127 return s;
@@ -1157,11 +1155,13 @@
11581156
11591157 // Alias $j to jQuery for backwards compatibility
11601158 window.$j = jQuery;
 1159+
 1160+// Global alias
11611161 window.mw = mediaWiki;
11621162
11631163 /* Auto-register from pre-loaded startup scripts */
11641164
1165 -if ( $.isFunction( startUp ) ) {
 1165+if ( jQuery.isFunction( startUp ) ) {
11661166 startUp();
11671167 delete startUp;
11681168 }

Sign-offs

UserFlagDate
Hasharinspected09:55, 11 August 2011

Status & tagging log