Performance of CSS Selectors Is Still Irrelevant
Post from November 29, 2017 (↻ June 2, 2021), filed under Web Development.
For a teaser, a snippet from my upcoming book on CSS optimization .
Selector performance is not something to optimize for. Selector performance is irrelevant. The price we pay for optimizing for selector performance is terrible: We micro-manage our work for gains that aren’t noticeable.
In 2009, Steve Souders contributed perhaps the first important results to the topic, and one of his main articles on the subject should have made more of us think. First, in his tests, even style sheets with 18,000 rules (!) were rendered within 600 ms (!). Then, Steve explained in quite clear terms, his hypothesis: “For most web sites, the possible performance gains from optimizing CSS selectors will be small, and are not worth the costs.”
(Years later, in additional investigations, we even find problems with the methodology, leading to important observations like this, by Ben Frain: “It is practically impossible to predict the final performance impact of a given selector by just examining the selectors.”)
The first problem with optimizing for selectors is that the gains are negligible. Yes, some selectors are slower than others, and from the way they work this is probably going to be the case until the world ends. Alas, nothing practically relevant follows from this observation: The effects are not felt by us nor by our end users. We do not ship websites that use 25,000 rules of the type of
:after (and that’s it) to bring pages to a crawl; a) they don’t come to a crawl, b) who writes code like that has others issues.
The second problem with optimizing for selectors is that we over-optimize and micro-manage, and actually slow down our workflow and write worse CSS. Putting up rules in place for how selectors should look like is adding a cognitive restriction that only hinders a team of developers, and the outcome is not any more elegant code in terms of minimalism (though those of us who connect verbosity with understandability may have a point).
Where we are right now, selector performance is still a box better left closed.
I’m Jens, and I’m an engineering lead—currently manager for Developer Experience at LivePerson—and author. I’ve worked as a technical lead for Google, I’m close to W3C and WHATWG, and I write and review books for O’Reilly. I love trying things, sometimes including philosophy, art, and adventure. Here on meiert.com I share some of my views and experiences.
If you have a question or suggestion about what I write, please leave a comment (if available) or a message. Thank you!
Maybe this is interesting to you, too:
- Next: An Ode to Smashing Magazine
- Previous: On Big Picture Thinking in Web Development
- More under Web Development, or from 2017
- Most popular posts
Looking for a way to comment? Comments have been disabled, unfortunately.
Get a good look at web development? Try The Web Development Glossary (2020). With explanations and definitions for literally thousands of terms from Web Development and related fields, building on Wikipedia as well as the MDN Web Docs. Available at Apple Books, Kobo, Google Play Books, and Leanpub.