From: Timo Tijhof Date: Sat, 8 Sep 2018 18:32:18 +0000 (+0100) Subject: resourceloader: Optimise mw.now() definition X-Git-Tag: 1.34.0-rc.0~4116^2 X-Git-Url: http://git.cyclocoop.org//%27%40script%40/%27?a=commitdiff_plain;h=7c66b040048874513722aedcd22eb30ce9169214;p=lhc%2Fweb%2Fwiklou.git resourceloader: Optimise mw.now() definition In a microbenchmark this wouldn't register given it's just property access, but the 'timing' and 'navigationStart' properties are non-trivial getter accessors on their first call, and that's worth deferring to when it is needed, instead of blocking the definition of mw.loader early on. Also remove the redundant wrapper around Date.now(), which is a static and otherwise detachable function, it does not need to be bound or wrapped. jQuery defines its shortcut the same way, as `jQuery.now = Date.now`. Change-Id: Id621b08fbcce886318bae76ea4c47d50fc9d88e9 --- diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index 57ac43ce4b..ba8869bd28 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -381,19 +381,24 @@ /** * Get the current time, measured in milliseconds since January 1, 1970 (UTC). * - * On browsers that implement the Navigation Timing API, this function will produce floating-point - * values with microsecond precision that are guaranteed to be monotonic. On all other browsers, - * it will fall back to using `Date`. + * On browsers that implement the Navigation Timing API, this function will produce + * floating-point values with microsecond precision that are guaranteed to be monotonic. + * On all other browsers, it will fall back to using `Date`. * * @return {number} Current time */ - now: ( function () { + now: function () { + // Optimisation: Define the shortcut on first call, not at module definition. var perf = window.performance, navStart = perf && perf.timing && perf.timing.navigationStart; - return navStart && typeof perf.now === 'function' ? + + // Define the relevant shortcut + mw.now = navStart && typeof perf.now === 'function' ? function () { return navStart + perf.now(); } : - function () { return Date.now(); }; - }() ), + Date.now; + + return mw.now(); + }, /** * List of all analytic events emitted so far.