From 4b069cdc1e4b408f240551be33940b751035d437 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 8 Sep 2018 19:01:45 +0100 Subject: [PATCH] resourceloader: Remove closure around $CODE.defineLoader() Follows-up b7b84d55d4e, which embedded the whole of the three 'mediawiki' JS files inside of startup.js, but did so inside of the pre-existing closure that was there. This adds some overhead we don't get value of, so best to remove it and embed it as a sibling of startup.js in the same top-level scope. In local testing (Chrome stable, CPU 1/6th) this reduced startup run-time from 73-78ms to 63-65ms. Also: * Declare isCompatible() as a normal function. Disable the implicit-globals warning given this isn't a regular module, this file is intentionally in the global scope. * Unfold the private startUp() function to its call site. Change-Id: Ida51ab20898c9e4ae6cbf7ad2968d88d824a1483 --- resources/src/startup/startup.js | 66 +++++++++---------- .../resourceloader/ResourceLoaderTest.php | 2 +- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/resources/src/startup/startup.js b/resources/src/startup/startup.js index 8c79c949b5..8e75535b2a 100644 --- a/resources/src/startup/startup.js +++ b/resources/src/startup/startup.js @@ -3,8 +3,8 @@ * * - Beware: This file MUST parse without errors on even the most ancient of browsers! */ -/* eslint-disable vars-on-top, no-unmodified-loop-condition */ -/* global isCompatible, $VARS, $CODE */ +/* eslint-disable no-implicit-globals, vars-on-top, no-unmodified-loop-condition */ +/* global $VARS, $CODE */ /** * See @@ -47,7 +47,7 @@ * @param {string} [str] User agent, defaults to navigator.userAgent * @return {boolean} User agent is compatible with MediaWiki JS */ -window.isCompatible = function ( str ) { +function isCompatible( str ) { var ua = str || navigator.userAgent; return !!( // https://caniuse.com/#feat=es5 @@ -74,39 +74,42 @@ window.isCompatible = function ( str ) { // Note: Please extend the regex instead of adding new ones !ua.match( /MSIE 10|webOS\/1\.[0-4]|SymbianOS|Series60|NetFront|Opera Mini|S40OviBrowser|MeeGo|Android.+Glass|^Mozilla\/5\.0 .+ Gecko\/$|googleweblight|PLAYSTATION|PlayStation/ ) ); -}; +} -( function () { - var NORLQ; +if ( !isCompatible() ) { // Handle Grade C - if ( !isCompatible() ) { - // Undo speculative Grade A class. See ResourceLoaderClientHtml::getDocumentAttributes(). - document.documentElement.className = document.documentElement.className - .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' ); + // Undo speculative Grade A class. See ResourceLoaderClientHtml::getDocumentAttributes(). + document.documentElement.className = document.documentElement.className + .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' ); - // Process any callbacks for Grade C - NORLQ = window.NORLQ; - while ( NORLQ && NORLQ[ 0 ] ) { - NORLQ.shift()(); + // Process any callbacks for Grade C + while ( window.NORLQ && window.NORLQ[ 0 ] ) { + window.NORLQ.shift()(); + } + window.NORLQ = { + push: function ( fn ) { + fn(); } - window.NORLQ = { - push: function ( fn ) { - fn(); - } - }; + }; - // Clear and disable the Grade A queue - window.RLQ = { - push: function () {} - }; + // Clear and disable the Grade A queue + window.RLQ = { + push: function () {} + }; +} else { + // Handle Grade A - return; + if ( window.performance && performance.mark ) { + performance.mark( 'mwStartup' ); } + // This embeds mediawiki.js, which defines 'mw' and 'mw.loader'. + $CODE.defineLoader(); + /** * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php. */ - function startUp() { + ( function () { mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals ); $CODE.registrations(); @@ -136,14 +139,5 @@ window.isCompatible = function ( str ) { window.NORLQ = { push: function () {} }; - } - - if ( window.performance && performance.mark ) { - performance.mark( 'mwStartup' ); - } - - // This embeds mediawiki.js, which defines 'mw' and 'mw.loader'. - $CODE.defineLoader(); - - startUp(); -}() ); + }() ); +} diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php index d791b7c259..9040f3974b 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php @@ -846,7 +846,7 @@ mw.example(); $this->assertRegExp( '/Ferry not found/', $errors[0] ); $this->assertCount( 1, $errors ); $this->assertRegExp( - '/isCompatible.*function startUp/s', + '/isCompatible.*window\.RLQ/s', $response, 'startup response undisrupted (T152266)' ); -- 2.20.1