Just throwing this question out here ’cause it got me thinking tonight.
I’ve heard before that it’s way better to include the HTML tag into a jQuery selector whenever you could, because it kicks up selection performance by, well, telling jQuery what HTML tags to look for so it doesn’t search everything in the document. (i.e. go $('div.someclass') instead of $('.someclass') when you could.)
My question is, how true is this now, with the current version of jQuery (currently 1.6.1)?
I’m not even sure if this notion came pre-Sizzle (not even sure if Sizzle helps jQuery perform better in this respect).
Of course, good coding practice would dictate that I include the HTML tag whenever I could just to keep things clear, but there’s a very gray area for me when I’d prefer to go
$('.someclass')
just because I don’t want to go
$('div.someclass, a.someclass, p.someclass, img:nth-child(3n+2).someclass')
Completely theoretical only dudes. I know I can just go $('div,a,p,img:nth-child(3n+2)').filter('.someclass').
EDIT
On a somewhat related note, I went ahead and made perf tests on with-tag vs without-tag selection (thanks Raynos), and seemingly, without-tag selection was significantly more beneficial than with-tag selection on FF and Chrome. with-tag was still faster on IE8, but by the looks of the narrow margin, it’s completely eclipsed by the alternative on other browsers. Hmm.
$('.someclass')Should be a lot faster then$("div.someclass, p.someclass")It’s only worthwhile being specific if it doesn’t broaden the query.
Benchmark