From a20333cc4da04775db04b33407c88b4ccb5e5e74 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Mon, 9 Dec 2013 22:35:37 -0800 Subject: [PATCH] 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 --- resources/mediawiki/mediawiki.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; -- 2.20.1