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

types: optimise ParseTReturn by reading count and incremental options #2095

Merged
merged 1 commit into from
Dec 30, 2023

Conversation

marcalexiei
Copy link
Member

I have used the ParseKeys type to do define some keys that are then passed to a t function and I noticed that when there are a lot of keys typescript was really slow (similar to #2070).

Looking at the types it looks like the bottleneck was ParseTReturnPlural.

To reduce build time I added the TOpt as a new type parameter so all plural and ordinal keys can be computed only when the relative option is provided to the t function. According to this ParseTReturnPlural has been divided into two separate types:

  • ParseTReturnPlural
  • ParseTReturnPluralOrdinal

Each one provide they key for the relevant plural scenario

https://github.com/marcalexiei/i18next/blob/70f498d128ccfdf10a3848981eae05ca548daed5/typescript/t.d.ts#L157-L169

type ParseTReturn<
  Key,
  Res,
  TOpt extends TOptions = {},
> = Key extends `${infer K1}${_KeySeparator}${infer RestKey}`
  ? ParseTReturn<RestKey, Res[K1 & keyof Res], TOpt>
  : // Process plurals only if count is provided inside options
  TOpt['count'] extends number
  ? TOpt['ordinal'] extends boolean
    ? ParseTReturnPluralOrdinal<Res, Key>
    : ParseTReturnPlural<Res, Key>
  : // otherwise access plain key without adding plural and ordinal suffixes
    Res[Key & keyof Res];

Results

running tic inside the the many-keys typescript test scenario produced the following results:

perf-compare

Build time was done by ~40%.

In a more simple test scenario like the one of test:typescript:customtypes the build time was improved by 5/10%

Test machine is a MacBook Pro (15-inch, 2016).

Support JSONFormat v3 in typescript

I added a new test scenario for typescript where JSONFormat v3 (which is the custom-types scenario with few key change for plural and ordinal keys).

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

@coveralls
Copy link

Coverage Status

coverage: 92.045%. remained the same
when pulling 70f498d on marcalexiei:feature/parse-t-return-perf
into 8ae4599 on i18next:master.

@adrai
Copy link
Member

adrai commented Dec 30, 2023

wow, this is awesome thank you very much...
Did you test it also with TS v4.x ?

@adrai adrai merged commit 0ad5cb5 into i18next:master Dec 30, 2023
7 checks passed
@adrai
Copy link
Member

adrai commented Dec 30, 2023

Did you test it also with TS v4.x ?

I just tested it myself... looks good.
released with v23.7.13

Grazie Marco ;-)

@marcalexiei
Copy link
Member Author

Sorry I was opening #2096 😅

Thank you for testing the change with TS 4.x!

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

Successfully merging this pull request may close these issues.

None yet

3 participants