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

chore: add inline new class warning #9583

Merged
merged 4 commits into from
Nov 22, 2023
Merged

chore: add inline new class warning #9583

merged 4 commits into from
Nov 22, 2023

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Nov 21, 2023

Creating classes inline can lead to significant performance memory problems and we should try and help people move away from this problematic pattern in favour of putting the class as a top-level module declaration, or just using object literals instead.

Copy link

changeset-bot bot commented Nov 21, 2023

🦋 Changeset detected

Latest commit: 0416fc8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Nov 21, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-5-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2023 2:40pm

const performance = {
'inline-new-class': () =>
`Creating inline classes will likely cause performance issues. ` +
`Instead, declare the class at the module-level and create new instances from the class reference.`
Copy link
Member

Choose a reason for hiding this comment

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

Should we have something in here like "or use getters/setters instead" because many people use it as a shortcut for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we want to tell people to use getters/setters here, or to just hoist their class?

@Rich-Harris

This comment was marked as resolved.

@benmccann
Copy link
Member

I was going to suggest this should be an eslint warning instead. Inline classes are simpler to write and there are a lot of basic projects out there that don't need the improved performance

@trueadm
Copy link
Contributor Author

trueadm commented Nov 22, 2023

@benmccann It's not just performance (however, if you are running without a JIT, i.e. on iOS in Chrome/FF/Facebook/TikTok etc, then the cost is gigantic), but there's quite a big memory impact from creating a new class each time. It's just a bad pattern that we shouldn't promote. If people are using inline classes because they're simpler to write, then we need to address that problem separately.

@Rich-Harris
Copy link
Member

If the heuristic we're using is 'declaration must be hoisted to component/module' rather than 'don't instantiate in the same scope as the declaration' then the warning should go on the declaration. I'll open a new PR

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

4 participants