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

[Polymorphic] Improve performance #784

Merged
merged 3 commits into from Jul 19, 2021
Merged

[Polymorphic] Improve performance #784

merged 3 commits into from Jul 19, 2021

Conversation

jjenzz
Copy link
Contributor

@jjenzz jjenzz commented Jul 16, 2021

It's really hard to know for sure if tweaks to polymorphic types are improving performance but I picked a huge component (ScrollArea) and recorded a video of me tweaking its types (with and without these changes) to show how TS is able to more quickly report issues now.

The main change here is to remove the extends keyof JSX.IntrinsicElements from the generic which would have been slowing down all components regardless of whether the as prop was being used.

Instead, I check to see if as extends an empty string and if so, I type it as keyof JSX.IntrinsicElements to enable intellisense when used.

INTELLISENSE

CleanShot.2021-07-16.at.19.30.35.mp4

BEFORE (~8s)

CleanShot.2021-07-16.at.19.13.12.mp4

AFTER (~5s)

CleanShot.2021-07-16.at.19.14.08.mp4

I'll continue to think of ways to improve it further but this definitely feels better.

@andy-hook
Copy link
Collaborator

Ran a quick test with --diagnostics and looks like this change is more than halving the check time 😮

image

packages/react/polymorphic/src/polymorphic.ts Show resolved Hide resolved
<As = IntrinsicElementString>(
props: As extends ''
? { as: keyof JSX.IntrinsicElements }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super familiar with these typings. Is the gist that the conditionals are progressively narrowing from simple to more complex only as necessary?

Copy link
Contributor Author

@jjenzz jjenzz Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and we do this instead of checking As extends React.ElementType (for both types) because that breaks event typing for intrinsics.

@jjenzz
Copy link
Contributor Author

jjenzz commented Jul 19, 2021

Ran a quick test with --diagnostics and looks like this change is more than halving the check time 😮

Never used that before, TIL 😅

Copy link
Collaborator

@andy-hook andy-hook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good from my side, works well and the perf improvement is mega 👍

@jjenzz jjenzz merged commit 63b54f0 into main Jul 19, 2021
@jjenzz jjenzz deleted the polymorphic-perf branch July 19, 2021 12:19
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.

None yet

2 participants