From 7c66b040048874513722aedcd22eb30ce9169214 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 8 Sep 2018 19:32:18 +0100 Subject: [PATCH] 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 --- resources/src/startup/mediawiki.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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. -- 2.20.1