Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance issue in Trans component #1646

Merged

Conversation

pedrodurek
Copy link
Member

@pedrodurek pedrodurek commented Jun 22, 2023

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 for KPrefix should be undefined. However, due to a TypeScript limitation, the KPrefix was not maintaining its default signature of undefined and instead was inferring all keys from the KeyPrefix. This caused the ParseKeys function to map over the entire list of keys and key prefixes in order to filter out the keys that started with every KPrefix value.

As a result, using the Trans component without specifying a KPrefix value could lead to an unnecessary exponential increase in the number of type inspections. This increase is due to the fact that ParseKeys 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 as Namespaces * 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.

Screenshot 2023-06-21 at 6 07 01 PM

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

Checklist (for documentation change)

  • only relevant documentation part is changed (make a diff before you submit the PR)
  • motivation/reason is provided
  • commit message and code follows the Developer's Certification of Origin

@coveralls
Copy link

Coverage Status

coverage: 95.98%. remained the same when pulling e70f6ae on pedrodurek:fix-performance-issue-trans-component into 8fc3eec on i18next:master.

@adrai adrai merged commit d1e762d into i18next:master Jun 22, 2023
@adrai
Copy link
Member

adrai commented Jun 22, 2023

thank you... ❤️
included in v13.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type instantiation is excessively deep and possibly infinite.ts(2589)
3 participants