perf(ivy): avoid unnecessary function call in i18n instruction (#31106)

Currently the `placeholders` parameter inside `i18nLocalize` is defaulted to `{}`, which means that we'll always hit the `Object.keys` call below. Since it's very likely that we won't have placeholders in the majority of strings, these changes add an extra guard so that we don't hit it on every invocation.

PR Close #31106
This commit is contained in:
Kristiyan Kostadinov 2019-06-18 16:29:08 +02:00 committed by Kara Erickson
parent 91008bd979
commit 029f1be204
1 changed files with 5 additions and 4 deletions

View File

@ -1316,13 +1316,14 @@ const LOCALIZE_PH_REGEXP = /\{\$(.*?)\}/g;
* @codeGenApi
* @deprecated this method is temporary & should not be used as it will be removed soon
*/
export function ɵɵi18nLocalize(input: string, placeholders: {[key: string]: string} = {}) {
export function ɵɵi18nLocalize(input: string, placeholders?: {[key: string]: string}) {
if (typeof TRANSLATIONS[input] !== 'undefined') { // to account for empty string
input = TRANSLATIONS[input];
}
return Object.keys(placeholders).length ?
input.replace(LOCALIZE_PH_REGEXP, (match, key) => placeholders[key] || '') :
input;
if (placeholders !== undefined && Object.keys(placeholders).length) {
return input.replace(LOCALIZE_PH_REGEXP, (_, key) => placeholders[key] || '');
}
return input;
}
/**