From: Ori Livneh Date: Tue, 10 Dec 2013 06:35:37 +0000 (-0800) Subject: Module storage: randomly choose between Function and $.globalEval X-Git-Tag: 1.31.0-rc.0~17680^2 X-Git-Url: http://git.cyclocoop.org//%22javascript:ModifierStyle%28%27%22.%24id.%22%27%29/%22?a=commitdiff_plain;h=a20333cc4da04775db04b33407c88b4ccb5e5e74;p=lhc%2Fweb%2Fwiklou.git Module storage: randomly choose between Function and $.globalEval V8 disables certain optimizations for eval()'d code because scope resolution is tricky. The same is not true for code compiled via Function(), which always runs in global scope. Let's see if Function() is faster. This patch makes ResourceLoader randomly use either Function() or $.globalEval. The choice is recorded in a boolean `useFunction` property on mw.loader.store so its value can be logged with load timing measurements. The logging will be done in WikimediaEvents. Bug: 58259 Change-Id: I7183778cb65c421ee19dcd61ee1dc0085f86bf10 --- diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 724ca5e814..840a071514 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -1269,7 +1269,12 @@ var mw = ( function ( $, undefined ) { } return true; } ); - $.globalEval( concatSource.join( ';' ) ); + if ( mw.loader.store.useFunction ) { + /* jshint -W054 */ + new Function( concatSource.join( ';' ) )(); + } else { + $.globalEval( concatSource.join( ';' ) ); + } } // Early exit if there's nothing to load... @@ -1816,6 +1821,7 @@ var mw = ( function ( $, undefined ) { raw = localStorage.getItem( mw.loader.store.getStoreKey() ); // If we get here, localStorage is available; mark enabled. mw.loader.store.enabled = true; + mw.loader.store.useFunction = !!Math.floor( Math.random() * 2 ); data = JSON.parse( raw ); if ( data && typeof data.items === 'object' && data.vary === mw.loader.store.getVary() ) { mw.loader.store.items = data.items;