Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJohn Michael Robin
3,750 PointsPerformance between classes and descendant selectors
In terms of performance and speed which is more recommended to use? classes or descendant selectors?
1 Answer
Steven Parker
229,732 PointsPerhaps someone with actual benchmark data will chime in, but it occurs to me that it might not always be one way or the other. The specific performance of both approaches might vary with what's actually on the page.
But just in general, simpler selectors will be more performant. So if you can do something with a single class as the selector, it would likely be faster than a descendant selector simply because it involves two (at least) components. Even so, I wouldn't add classes strictly for performance purposes.
Also, after you see what some of the popular frameworks do with selectors, these kinds of comparisons might not seem very significant.
John Michael Robin
3,750 PointsJohn Michael Robin
3,750 PointsThanks for the input. I was just wondering whether it was better to just assign classes to children and descendants, or just use the descendant selectors since classes tend to make the markup a bit messier.
Steven Parker
229,732 PointsSteven Parker
229,732 PointsThat's why I said "I wouldn't add classes strictly for performance purposes.".