Tool
Google Chrome had a CSS profiler , but this was removed because it is considered that CSS performance today is reasonably good for cases that were slow a few years ago, so simply this type of optimization is something that is not worth investing.
However, you can still see the time that styles are interpreted and recalculated for the page using the developer tool timeline.
By accessing the Timeline tab of the developer tool, start the capture by clicking the ball in the upper left corner, reloading the page being tested, and then click the ball again to finalize the data capture. Leave JS Profile checked.
Parse Stylesheet will give you the time it took for the browser to interpret the styles and > Recalculate style the time it took to apply the style.
See the example below:
Interpretingtheresults
Inthefigure,weseethatthestylesheetall.css
ofthispagetook%of%millisecondstobeinterpreted.Nextthereisanotherspecificstylesheetformoderators.:)
Afterthesheetsareprocessed,anumberofstylecalculationeventsoccur,butunfortunatelytheydonotcontaintheinformationofwhichrulestheyareapplying.
Foramoreeffectivetest,itwouldbenecessarytoisolatethestylestomakesuretheyarerunning.Itisamanualwork,butwouldallowcomparisonstobemade.
Ontheotherhand,comparingtimesbetweenspecificselectorsdoesnotmakemuchsensebecauseofseveralfactors,including:
Implementationdependency
Eachbrowseranditsdifferentversionscanbeoptimized(ornot)inverydifferentways.
ThinkofCSSselectorsasacontract,anAPI,wherethereisnoguaranteeofhowtheimplementationismadeorevenmodified.
Possibleoptimizations
IfIweretoimplementamechanismtoapplyastylesheetintheDOMIwouldnotalwaysgothroughtheselectorselectorandforeachofthem,lookfortheelementsintheDOM,traversingthecountlesstreetimes.
First,Iwouldsaveid'sandclassesonmapssothataccesswasmadeinrelativelyconstanttimeregardlessofthenumberofelements.Idonotknowhowthisisactuallydoneineverybrowser,butIwillassumethatdevelopersarenowusingunsuitabledatastructures,scrollingthroughlistsortreesunnecessarilyatalltimes.
Second,evenincaseswhereitisnecessarytogothroughtheDOM,youcoulduseapatternlikeVisitorandapplymultiplestylesatthesametimeinbatch,sototaltimetoapplytwoselectorstogetherwasmuchlessthanthesumofthetimeofapplyingthetwoseparately.
Considerationoftotaltime
It'snotjustthetimetoapplyaselectorthatcounts.Asintheexampleabove,itisnecessarytoconsiderthestylesheetinterpretationtime.
Probablytheinterpretationofmanyindividualid'swilltakeamuchlongertimethantheinterpretationofasingleclass.
Inaddition,ifthebrowserhassomesortofcacheordatastructurelikethemapIdescribedabove,retrievingthelistofelementsthatcontainagivenclasscanbemuchfasterthanqueryingathousandtimesthemaplookingforid's
Considerations
First,whenawebsiteneedsoptimization,weshouldfirstlookatwhatcausesthegreatestimpactandforgetaboutpossiblemicro-optimizations.
Inthemulti-browserdevelopertool,youcaneasilyfindoutwhicheventsareslowerandleavetheuserblockedforlongerandthenlookforthecause.Here'sanexampleofanotherChromechartshowingthetimeofeachpageloadevent:
Second, almost never rely on performance claims that are based on ever-changing implementation details. Of course, there may be specific cases where we run into bugs or bizarre behavior and we need to work around the situation in some way. However, it is more appropriate to treat them as temporary solutions and not try to turn them into good practices.