perf(engine-dom): use adoptedStyleSheets.push #2683
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
Chrome recently made
adoptedStyleSheets
mutable (see this post and WICG/construct-stylesheets#45). The idea is that, instead of doing:...you can do:
Benchmarking
In prior testing (WICG/construct-stylesheets#94 (comment)), I indeed found some overhead from cloning and re-assigning the array. This is actually why we don't use
adoptedStyleSheets
at thedocument
level – it tends to have so many stylesheets that the perf cost of repeatedly cloning the array is too high.Just to be sure, I wrote a new microbenchmark. It confirms that calling
push
is indeed faster than re-assigning the array. With 1000 components and 100 stylesheets per component, testing the median of 5 runs on Chrome Canary 100, I got 4491.3ms (re-assignment) vs 334.5ms (push), so it's a pretty big difference.Feature detection
Unfortunately, there doesn't seem to be a straightforward way of testing that
adoptedStyleSheets.push
is supported. In the old (frozen) array, it has all of the mutation methods (push
,shift
,pop
, etc.), but if you call them, they throw an error:The most succinct detection method I found is:
This is correctly
true
in Chrome Canary 100 andfalse
in Chrome 97. It's also correctlyfalse
in Firefox Nightly 98 with the constructable stylesheets flag enabled.Does this pull request introduce a breaking change?
Does this pull request introduce an observable change?