From 6bb5b01b48a17c1e764046bb28e1194e3783034c Mon Sep 17 00:00:00 2001 From: dsemenov Date: Sun, 29 Dec 2019 17:55:35 -0800 Subject: [PATCH] [perf] Cache loadLocale misses to avoid FS ops --- src/lib/locale/locales.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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]; } -- 2.47.2