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

JavaScript

Doe CSS efficiency correlate with CSS selectors used in jQuery?

After learning about CSS efficiency I've became mindful (stubborn) with applying styles to class selectors as much as possible.

I was wondering if CSS selector in jQuery is also subject to such efficiency?

e.g.:

is

$('nav li')

less efficient than

$('.nav .nav-item')

?

Or am I simply being obsessively pedantic?

2 Answers

When it comes to jQuery (Sizzle) selectors, the fastest are id selectors and tag selectors. Classes are a bit tricky. Older versions of IE (prior to version 9) did not have document.getElementsByClassName, so if running on such a browser, the class selector will be the slowest one.

jQuery documentation actually has a section on optimizing selectors.

That's very helpful, thanks very much :)