inlineCriticalCss is slow #2106
Description
🐞 Bug report
What modules are related to this issue?
- aspnetcore-engine
- builders
- common
- express-engine
- hapi-engine
Is this a regression?
No
Description
Recently I tried to enable inlineCriticalCss on our project. And had to roll it back quickly - SSR times jumped sky high.
styles.css
in our project is around 250KB. This includes Angular Material (core + theme) and a number of old-style components that have not been converted to styleUrls
yet. However even if I strip it down to bare minumum - it's still ~100KB (majority of that are Material styles).
HTML is also somewhat significant - 200KB (after I stripped it off of transfered state), and contain 1500 DOM nodes.
Profiling shows that most of the css inlining time is spent on findOne
call.
My understanding is that critters
has complexity roughly O(N * S)
, where N is number of DOM nodes, and S is number of selectors in the stylesheets. That's because it queries every selector against the DOM. And our stylesheet (the minimal version) has 1000+ selectors. The regular prod version of the stylesheet - 2600 selectors.
I'm wondering if it could be improved by grouping selectors and discarding a whole group if the top-most selector was not found. E.g if we checked .mat-button
selector and didn't find any matching node - there is no point in looking for more specific selectors like .mat-button .mat-button-focus-overlay
🔬 Minimal Reproduction
🌍 Your Environment
Angular CLI: 11.2.12
Node: 12.14.1
OS: darwin x64
Angular: 11.2.13
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router
Ivy Workspace: Yes
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1102.12
@angular-devkit/build-angular 0.1102.12
@angular-devkit/core 11.2.12
@angular-devkit/schematics 11.2.12
@angular/cdk 11.2.12
@angular/cli 11.2.12
@angular/material 11.2.12
@nguniversal/builders 11.2.1
@nguniversal/express-engine 11.2.1
@schematics/angular 11.2.12
@schematics/update 0.1102.12
rxjs 6.6.3
typescript 4.1.5