perf: optimize initial render for nullish class #4380
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Details
From looking at our Best benchmarks, I realized we had actually regressed recently in
dom-table-hydrate-1k
due to #3950:Tachometer confirmed it (comparing current master to the 250 release, i.e. v6.4.5):
The reason is actually unrelated to hydration – it's because the components used in this benchmark set a
class
torow.className
which is actually undefined:lwc/packages/@lwc/perf-benchmarks-components/src/benchmark/table/table.html
Line 11 in c1f4a2b
After #3950, the
ncls
utility is convertingundefined
into an empty string:lwc/packages/@lwc/engine-core/src/framework/api.ts
Lines 747 to 748 in c1f4a2b
lwc/packages/@lwc/engine-core/src/framework/api.ts
Line 770 in c1f4a2b
This means we don't hit the early-return condition when patching the class attribute:
lwc/packages/@lwc/engine-core/src/framework/modules/computed-class-attr.ts
Lines 70 to 73 in c1f4a2b
This PR fixes this, by converting
undefined
andnull
intoundefined
withinncls
, which means we hit the early-return when patching classes. This fixes the perf regression in the benchmark (comparing this PR tomaster
):There are probably more optimizations we can do to
ncls
(e.g. treating''
the same asundefined
, avoiding theStringTrim
call), but this PR is at least a start.Does this pull request introduce a breaking change?
Does this pull request introduce an observable change?
The engine ultimately treats a
null
,undefined
, or""
class exactly the same at runtime, so there are no observable changes.