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 trial

CSS

John Michael Robin
John Michael Robin
3,750 Points

Performance 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
Steven Parker
229,732 Points

Perhaps 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. :wink:

John Michael Robin
John Michael Robin
3,750 Points

Thanks 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
Steven Parker
229,732 Points

That's why I said "I wouldn't add classes strictly for performance purposes.".