From afbf6423de0098142feec25c189334d8d0a2c80f Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 24 Mar 2021 16:31:27 +0100 Subject: [PATCH 1/2] feat: improve performance of main function --- index.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 0a9899b..9adc29c 100644 --- a/index.js +++ b/index.js @@ -135,22 +135,32 @@ if (platform === 'linux') { // TODO: Use https://github.com/sindresorhus/is-unicode-supported when targeting Node.js 10. const figures = platform === 'win32' ? fallback : main; -const fn = string => { - if (figures === main) { - return string; +const isFallbackFigure = ([key, mainValue]) => figures[key] !== mainValue; +const getFigureRegExp = ([key, mainValue]) => [figures[key], new RegExp(escapeStringRegexp(mainValue), 'g')]; + +let replacements = []; +const getReplacements = () => { + if (replacements.length !== 0) { + return replacements; } - for (const [key, value] of Object.entries(main)) { - if (value === figures[key]) { - continue; - } + replacements = Object.entries(main) + .filter(isFallbackFigure) + .map(getFigureRegExp); + return replacements; +}; - string = string.replace(new RegExp(escapeStringRegexp(value), 'g'), figures[key]); +const replaceCharToFallback = (string, [fallbackValue, mainRegExp]) => string.replace(mainRegExp, fallbackValue); + +// On Windows, substitute non-fallback to fallback figures +const replaceCharsToFallback = string => { + if (figures === main) { + return string; } - return string; + return getReplacements().reduce(replaceCharToFallback, string); }; -module.exports = Object.assign(fn, figures); +module.exports = Object.assign(replaceCharsToFallback, figures); module.exports.main = main; module.exports.windows = fallback; From 87de1e79df0131ffd308c9607919a5d4f35f51da Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 24 Mar 2021 18:48:51 +0100 Subject: [PATCH 2/2] chore: do not use `Array.reduce()` --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9adc29c..c06a11d 100644 --- a/index.js +++ b/index.js @@ -136,7 +136,7 @@ if (platform === 'linux') { const figures = platform === 'win32' ? fallback : main; const isFallbackFigure = ([key, mainValue]) => figures[key] !== mainValue; -const getFigureRegExp = ([key, mainValue]) => [figures[key], new RegExp(escapeStringRegexp(mainValue), 'g')]; +const getFigureRegExp = ([key, mainValue]) => [new RegExp(escapeStringRegexp(mainValue), 'g'), figures[key]]; let replacements = []; const getReplacements = () => { @@ -150,15 +150,17 @@ const getReplacements = () => { return replacements; }; -const replaceCharToFallback = (string, [fallbackValue, mainRegExp]) => string.replace(mainRegExp, fallbackValue); - // On Windows, substitute non-fallback to fallback figures const replaceCharsToFallback = string => { if (figures === main) { return string; } - return getReplacements().reduce(replaceCharToFallback, string); + for (const [mainRegExp, fallbackValue] of getReplacements()) { + string = string.replace(mainRegExp, fallbackValue); + } + + return string; }; module.exports = Object.assign(replaceCharsToFallback, figures);