From 42bc7942038fe4c5f103edab00e6a8203a96f378 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Tue, 14 Aug 2018 01:39:45 +0100 Subject: [PATCH] resourceloader: Optimise getCombinedVersion() with string concat MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is used many times in the hot path. The main motivation for this is to reduce memory churn in that path. By building up a string directly instead of first mapping into an array, and than using Array#join() - we avoid some of that churn. It terms of speed, avoiding Array#join resulted in the same or better performance for the browsers I tested. | | Chrome 60 | Safari 11.1 | Firefox 61 | map+join | 241K/s (± 2%) | 189K/s (± 5%) | 161K/s (± 6%) | forEach+concat | 297K/s (± 1%) | 398K/s (± 2%) | 159K/s (± 13%) | reduce | 437K/s (± 2%) | 396K/s (± 1%) | 138K/s (± 12%) For Chrome and Safari, 'reduce' were marked "fastest". For Firefox, JSPerf marked all three as fastest due to the variable ranges overlapping. Re-runs produced similar results, each time with three cases laid out in a slightly different order in the 130-165K/s range. The test used a copy of en.wikipedia's current registry with the module queue of a random article. https://jsperf.com/array-map-and-join-vs-string-concat/1 Change-Id: Ifbd8f6509ac118657c5ad2833f54fd6e8e63f9ce --- resources/src/startup/mediawiki.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index d586fc9f9f..f9a69b82ce 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -727,10 +727,10 @@ * @return {string} Hash of concatenated version hashes. */ function getCombinedVersion( modules ) { - var hashes = modules.map( function ( module ) { - return registry[ module ].version; - } ); - return fnv132( hashes.join( '' ) ); + var hashes = modules.reduce( function ( result, module ) { + return result + registry[ module ].version; + }, '' ); + return fnv132( hashes ); } /** -- 2.20.1