From: dsemenov Date: Mon, 30 Dec 2019 01:55:35 +0000 (-0800) Subject: [perf] Cache loadLocale misses to avoid FS ops X-Git-Tag: 2.25.0~61^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6bb5b01b48a17c1e764046bb28e1194e3783034c;p=thirdparty%2Fmoment.git [perf] Cache loadLocale misses to avoid FS ops --- diff --git a/src/lib/locale/locales.js b/src/lib/locale/locales.js index e3e87d26d..86b933d66 100644 --- a/src/lib/locale/locales.js +++ b/src/lib/locale/locales.js @@ -47,14 +47,18 @@ function chooseLocale(names) { function loadLocale(name) { var oldLocale = null; // TODO: Find a better way to register and load all the locales in Node - if (!locales[name] && (typeof module !== 'undefined') && + if (locales[name] === undefined && (typeof module !== 'undefined') && module && module.exports) { try { oldLocale = globalLocale._abbr; var aliasedRequire = require; aliasedRequire('./locale/' + name); getSetGlobalLocale(oldLocale); - } catch (e) {} + } catch (e) { + // mark as not found to avoid repeating expensive file require call causing high CPU + // when trying to find en-US, en_US, en-us for every format call + locales[name] = null; // null means not found + } } return locales[name]; }