From 029f1be20407ce14b12605cae8409445022be70e Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 18 Jun 2019 16:29:08 +0200 Subject: [PATCH] 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 --- packages/core/src/render3/i18n.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core/src/render3/i18n.ts b/packages/core/src/render3/i18n.ts index 5c0f9c28e6..2700cce34d 100644 --- a/packages/core/src/render3/i18n.ts +++ b/packages/core/src/render3/i18n.ts @@ -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; } /**