Fix performance issue in Trans
component
#1646
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #1601
Identifying the source of the problem was challenging, but thanks to a reproducible example shared by @sebastien-comeau, I was able to identify the root cause.
By default, when the
t
function is not passed, the inferred value forKPrefix
should beundefined
. However, due to a TypeScript limitation, theKPrefix
was not maintaining its default signature ofundefined
and instead was inferring all keys from theKeyPrefix
. This caused theParseKeys
function to map over the entire list of keys and key prefixes in order to filter out the keys that started with everyKPrefix
value.As a result, using the
Trans
component without specifying aKPrefix
value could lead to an unnecessary exponential increase in the number of type inspections. This increase is due to the fact thatParseKeys
would have to perform a large number of inspections for every possible combination of namespaces, keys, and key prefixes. Specifically, this increase in inspections can be characterized asNamespaces * Keys * Key Prefix
.To avoid this performance issue, I remove the constraint value from
KPrefix
(which, in this case, brings no benefit). By removing the constraint value,KPrefix
maintains its default value (undefined
) and avoids unnecessary inspections.Checklist
npm run test
Checklist (for documentation change)