Skip to content

Commit c9cde3a

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
perf(platform-browser): only append style element on creation (#52237)
Prior to this change the style element was appended to host element multiple times. Whilst the actual element was not added multiple to the DOM multiple times. This causes a performance regression and it caused repainting. This can be observed in the below benchmark. ```js (() => { const time = (name, fn) => { const t = performance.now(); fn(); console.log(name, performance.now() - t); } const s = document.createElement("style"); s.textContent = "@layer x{} @font-face { font-family: foo; }"; time("append and enable", () => { document.head.append(s); s.disabled = false; }); time("compute body color", () => { getComputedStyle(document.body).color; }); time("compute body layout", () => { document.body.offsetTop; }); time("append and disable", () => { document.head.append(s); s.disabled = false; }); time("compute body color", () => { getComputedStyle(document.body).color; }); time("compute body layout", () => { document.body.offsetTop; }); })(); ``` Output ``` append and enable 0.20000000298023224 compute body color 0.7999999970197678 compute body layout 2.899999998509884 append and disable 0.10000000149011612 compute body color 0.7000000029802322 compute body layout 2.2999999970197678 ``` When commenting the 2nd `document.head.append(s);`, the results are slightly different and we can see that calling `getComputedStyle` does not incur any performance impact this is a result of no repainting. ``` append and enable 0.10000000149011612 compute body color 0.7999999970197678 compute body layout 3.1999999955296516 append and disable 0.10000000149011612 compute body color 0 compute body layout 0 ``` Pantheon benchmarks: http://docs/spreadsheets/d/1iLRLGCmVYZHuVRdI7dO_WM7wnQ1DvkS-tJzi-0-u1KY?resourcekey=0-kwtrf0nbAhcPqAGdqbdz4g#gid=0 PR Close #52237
1 parent 35fd10a commit c9cde3a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

packages/platform-browser/src/dom/shared_styles_host.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export class SharedStylesHost implements OnDestroy {
139139
if (existingStyleElement) {
140140
return existingStyleElement;
141141
}
142+
142143
const styleNodesInDOM = this.styleNodesInDOM;
143144
const styleEl = styleNodesInDOM?.get(style);
144145
if (styleEl?.parentNode === host) {
@@ -166,6 +167,8 @@ export class SharedStylesHost implements OnDestroy {
166167
styleEl.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId);
167168
}
168169

170+
host.appendChild(styleEl);
171+
169172
return styleEl;
170173
}
171174
}
@@ -175,8 +178,6 @@ export class SharedStylesHost implements OnDestroy {
175178
const styleResult = styleRef.get(style)!; // This will always be defined in `changeUsageCount`
176179
const styleEl = this.getStyleElement(host, style, styleResult.elements);
177180

178-
host.appendChild(styleEl);
179-
180181
if (styleResult.usage === 0) {
181182
disableStylesheet(styleEl);
182183
} else {

0 commit comments

Comments
 (0)