Merge "resourceloader: Optimise mw.now() definition"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 12 Sep 2018 10:29:32 +0000 (10:29 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 12 Sep 2018 10:29:32 +0000 (10:29 +0000)
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.