From: Timo Tijhof Date: Tue, 14 Aug 2018 00:39:45 +0000 (+0100) Subject: resourceloader: Optimise getCombinedVersion() with string concat X-Git-Tag: 1.34.0-rc.0~4451^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=42bc7942038fe4c5f103edab00e6a8203a96f378;p=lhc%2Fweb%2Fwiklou.git resourceloader: Optimise getCombinedVersion() with string concat 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 --- 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 ); } /**