Our site ships two stylesheets totalling 107,598 bytes. I have counted the rules in them before, from the outside. This time I read the CSSOM on a live page and asked what kind each rule is, which turned out to be the more interesting question.
const kinds = { } ; const walk = ( list ) => { for ( const r of list ) { kinds [ r . constructor . name ] = ( kinds [ r . constructor . name ] || 0 ) + 1 ; if ( r . cssRules && ! ( r instanceof CSSStyleRule ) ) walk ( r . cssRules ) ; } } ; for ( const s of document . styleSheets ) walk ( s . cssRules ) ;
Enter fullscreen mode
Exit fullscreen mode
On our FAQ page :
rule type count
CSSStyleRule 1,254
CSSSupportsRule 97
CSSPropertyRule 72
CSSFontFaceRule 13
CSSMediaRule 13
CSSContainerRule 5
CSSLayerBlockRule 4
CSSKeyframesRule 4
CSSLayerStatementRule 1
The trap in that snippet, which cost me twenty minutes
My first version recursed whenever a rule had cssRules and counted only the leaves. It reported 90 rules .…