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

perf: optimize initial render for nullish class #4380

Merged
merged 1 commit into from
Jul 10, 2024
Merged

Conversation

nolanlawson
Copy link
Collaborator

Details

From looking at our Best benchmarks, I realized we had actually regressed recently in dom-table-hydrate-1k due to #3950:

Screenshot 2024-07-10 at 11 10 54 AM

Tachometer confirmed it (comparing current master to the 250 release, i.e. v6.4.5):

Screenshot 2024-07-10 at 12 36 49 PM

The reason is actually unrelated to hydration – it's because the components used in this benchmark set a class to row.className which is actually undefined:

After #3950, the ncls utility is converting undefined into an empty string:

function ncls(value: unknown): string {
let res = '';

return StringTrim.call(res);

This means we don't hit the early-return condition when patching the class attribute:

const oldClass = isNull(oldVnode) ? undefined : oldVnode.data.className;
if (oldClass === newClass) {
return;
}

This PR fixes this, by converting undefined and null into undefined within ncls, which means we hit the early-return when patching classes. This fixes the perf regression in the benchmark (comparing this PR to master):

Screenshot 2024-07-10 at 12 36 40 PM

There are probably more optimizations we can do to ncls (e.g. treating '' the same as undefined, avoiding the StringTrim call), but this PR is at least a start.

Does this pull request introduce a breaking change?

  • 😮‍💨 No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • 🤞 No, it does not introduce an observable change.

The engine ultimately treats a null, undefined, or "" class exactly the same at runtime, so there are no observable changes.

@nolanlawson nolanlawson requested a review from a team as a code owner July 10, 2024 19:59
Copy link
Member

@jmsjtu jmsjtu left a comment

Choose a reason for hiding this comment

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

Nice find! 😄 LGTM

@nolanlawson nolanlawson merged commit a2c8607 into master Jul 10, 2024
11 checks passed
@nolanlawson nolanlawson deleted the nolan/class-perf branch July 10, 2024 22:10
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.

2 participants