Skip to content

perf: use constructable stylesheets in native shadow - #2290

Closed
nolanlawson wants to merge 2 commits into
salesforce:masterfrom
nolanlawson:nolan/constructable-stylesheets-3
Closed

nolanlawson wants to merge 2 commits into
salesforce:masterfrom
nolanlawson:nolan/constructable-stylesheets-3

Conversation

@nolanlawson

@nolanlawson nolanlawson commented Apr 9, 2021 •

Copy link
Copy Markdown
Contributor

Details

Background:

This PR requires #2289 because the snapshots need to be updated. The relevant changes are just: 0a6dfb7

Based on a small benchmark of rendering the same element 1,000 times, with that element containing a 1,000-line CSS file (a somewhat extreme example, but good for a benchmark), the end-to-end rendering time was improved from ~380ms to ~180ms (on my MacBook Pro).

The main savings come from not needing to create a <style> element for each component. Instead, we just append a CSSStyleSheet object to cmp.shadowRoot.adoptedStyleSheets.

I also went ahead and added an optimization for the stylesheet() function to cache the output (which never changes in the case of native shadow), but this is less of a huge improvement unless the CSS file for the component is large (in which case the string concatenation takes a long time). But it seems sensible to do this anyway so we don't need to create CSSStyleSheet objects over and over again, or have some kind of system for de-duping them.

Does this PR introduce breaking changes?

  • ✅ No, it does not introduce breaking changes.

The PR fulfills these requirements:

  • Have tests for the proposed changes been added? ✅
  • Have you followed these instructions to clearly describe the issue being fixed or feature enhanced? ✅

GUS work item

@W-9125079

This PR migrates some tests to use [Jest snapshots](https://jestjs.io/docs/snapshot-testing). This makes it easier to update these tests, as you can just run `yarn test -u` and the snapshot files update automatically.

The downside of this PR is that the "expected" output files are now in Jest snapshot format rather than plaintext format. This may make it a bit harder to edit or inspect manually.
return ${serializedStyle};
}

function stylesheet(${HOST_SELECTOR_IDENTIFIER}, ${SHADOW_SELECTOR_IDENTIFIER}, ${SHADOW_DOM_ENABLED_IDENTIFIER}, ${HAS_ADOPTED_STYLESHEETS_IDENTIFIER}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be interesting to move this into a helper in @lwc/engine-core to avoid code duplication.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure how to do that; it seems the import I add is left unmodified in the final code output. 86cd065

If you have some advice about how to do this, that would help me a lot. :)

if (expectedSource) {
expect(result).toBe(expectedSource);

if (error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let remove this change from this PR as it is discussed in #2289.

@@ -0,0 +1,30 @@
import { createElement } from 'lwc';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be covered by: lwc/packages/integration-karma/test/shadow-dom/stylesheet/index.spec.js

@nolanlawson

Copy link
Copy Markdown
Contributor Author

Will close with a version based on #2292

@nolanlawson

Copy link
Copy Markdown
Contributor Author

Closed in favor of a new PR that's not from a fork ^

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