CSS optimization is a misleading subject. On the one hand, it appears like one thing easy that we should always have already got all of the tooling for — particularly relating to minification. However, the concept we already exploited “every thing” there may be round CSS optimization could also be thought-about naive. Such a declare appears laborious to defend for a language that evolves virtually day by day.
On this article, I’d like to select a way that’s at the least 18 years outdated — UDJO, or “utilizing declarations simply as soon as.”
Conscious of an older supply? Please remark!
The approach is impressed by the precept of DRY, “don’t repeat your self”, in that it really works by figuring out and decreasing (if not eliminating) declaration repetition.
So, if there’s one thing like show: flex and it seems two or extra instances, it will get reconciled in a single rule:
.instance {
show: flex;
}
.pattern {
show: flex;
}
…turns into:
.instance,
.pattern {
show: flex;
}
The result of the approach will depend on how intelligently the deduplication is completed and the way lengthy the selectors are. Intelligence determines if UDJO results in an precise maintainability benefit — can the change of a declaration actually be completed to all of the selectors hooked up? And the selector size influences how huge file measurement financial savings are.
For those who’re , my article on “DRY CSS” describes the mechanics in additional element. The query right here is: Is UDJO nonetheless efficient and nonetheless related at the moment?
After we attempt to reply this query, nevertheless, we find yourself with an issue: When websites are complicated and their model sheet declarations haven’t been de-duplicated earlier than, UDJO can require a number of work.
To avoid wasting time, I constructed tooling for this work. Whereas it didn’t seem to be it will instantly be as complete as manually going by and testing model sheets, it appeared reasonable sufficient {that a} device may catch some repetition.
Skipping forward, I created CSS Dedup not solely to determine repetition, but in addition to deduplicate what may safely be optimized (CSS Dedup has some documentation on this however in essence, deduplication impacts selector places, which is a part of how the cascade works). This, then, provides us some knowledge:
Is There Something to Deduplicate in Fashionable CSS?
For the primary batch, I arbitrarily picked 20 web sites to research. I then used CSS Dedup to test what the file measurement adjustments can be if we deduplicated what appeared provably secure, and likewise what appeared most likely secure, however provided that it led to file measurement financial savings.
I picked these two choices to supply the “pure” results of deduplication, but in addition the one web site house owners could also be most curious about to scale back repetition but in addition lower switch measurement. In spite of everything, UDJO as a CSS optimization approach can enhance each maintainability and efficiency.
Listed here are the findings:
Essential observe: These values can’t be used to inform CSS high quality—they depend upon selector size and likewise on verifying if deduplication, within the respective case, truly results in higher maintainability. Additionally observe that this leaves out adjustments after compression—this was not a part of the experiment however can be truthful to check, too.
What can we inform from this?
- In the entire 20 circumstances, we’re capable of cut back declaration repetition.
- In 18 circumstances, decreasing declaration repetition results in a file measurement improve (resulting from selector repetition and selectors being longer than the declarations saved).
- Nonetheless, in all circumstances, declaration repetition will be decreased in a way that additionally reduces file measurement (by advantage of solely deduplicating model sheets that result in such financial savings).
For the query whether or not UDJO nonetheless works at the moment, this needs to be a sure. It really works in addition to to even include file measurement financial savings.
Perhaps you’re curious, too, about what the state of affairs appears like in our area, amongst net builders. Let’s take some samples on this, too.
What does this knowledge present?
- Simply as with organizations, on each web site there’s some declaration repetition secure to take away.
- In 5 circumstances (50% in comparison with 90%), decreasing repetition results in a file measurement improve.
- But in all circumstances once more, repetition may also be decreased in a manner that results in file measurement financial savings.
It will be priceless to increase this evaluation to take extra samples and examine the variations throughout samples, however there could also be one thing to net professionals utilizing shorter selectors in order to have the ability to profit extra from UDJO.
On the Effectiveness of UDJO as a CSS Optimization Approach
As talked about firstly, UDJO’s effectiveness usually will depend on the type of repetition in addition to on the size of selectors.
This may be managed, nevertheless: Whereas instruments like CSS Dedup make no distinction between declaration use circumstances (and are due to this fact blunt), it’s doable to configure them in order to reap the efficiency advantages.
As the information taken for this text counsel, file measurement financial savings could also be achieved normally. Contemplating that that is primarily based on secure adjustments, this implies a low-cost efficiency optimization vector — particularly when utilized to minified output. It’s nonetheless a good suggestion to sofa that towards the usage of additional compression (e.g. gzip/Brotli), however at face worth, deduplication exhibits the way it can cut back the dimensions of CSS.
For anybody usually curious about CSS optimization, maintainability, and efficiency, UDJO ought to most likely be a part of the general strategy to CSS. However for the reason that approach has been area of interest and since tooling is new, I invite you to overview, take a look at, and discover this vector. It appears laborious to argue that we’ve got reached the ceiling of how far we will take CSS optimization.







