-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: memoize clsx() #15441
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: memoize clsx() #15441
Conversation
🦋 Changeset detectedLatest commit: ca3e432 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
Good catch. I think we can get away without a test here (if we wanted to add one it would be in the I noticed that it doesn't memoize expressions that already involve memoization — this... <div class={foo()}></div> ...becomes this... $.template_effect(($0) => $.set_class(div, 1, $.clsx($0)), [() => $$props.foo()]); ...rather than this: $.template_effect(($0) => $.set_class(div, 1, $0), [() => $.clsx($$props.foo())]); No idea off the top of my head if that's avoidable? |
Yes that may come with more complex case to handle, so I skipped it for now. $.template_effect(
($0) => {
$.set_class(div, 1, $.clsx($0));
$.set_class(div_1, 1, $.clsx($0));
},
[() => $$props.foo()]
); Perhaps we can use const expression = $.derived(() => $$props.foo());
$.template_effect(
($0) => {
$.set_class(div, 1, $0));
$.set_class(div_1, 1, $0);
},
[() => $.clsx(expression)]
); But it may be a more complex change, as currently |
To be honest we should probably remove the deduplication anyway, per #15096 — will open a PR for that |
Without the deduplication it would be easier to do ! |
Merged #15451 so should be good to go |
I got mixed up with git, so I made another branch/PR : #15456 |
Just a little improvement on clsx().
Function call in the template are memoized in order to be executed only on state changes.
But this is not the case for
$.clsx()
which is added by the compiler.Currently, a code like that :
is compiled into something like that :
So, every time the template is updated,
$.clsx($$props.class)
is re-evaluated.With this PR, the
$.clsx($$props.class)
will be memoized on thetemplate_effect()
, and executed only when necessary :Note that this is only done when the value of
$.clsx()
is not already memoized (I think this is a rare case which would be more complex to treat).I don't have a test for that (I don't known how to check that), but all the current test are OK.
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint