resourceloader: Optimise mw.now() definition
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 8 Sep 2018 18:32:18 +0000 (19:32 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Sat, 8 Sep 2018 21:45:03 +0000 (22:45 +0100)
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

index 57ac43c..ba8869b 100644 (file)
                /**
                 * 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.