Is this faster:
$(document.links).filter('a.someClass')
than just plain old this:
$('a.someClass')
?
I don’t see anywhere in jQuery’s code the utilization of document.links
which gives you the collection of links on the document right away,
than, it would seem, it would be faster to just filter in the collection
instead of all the DOM nodes, which is alot more nodes to go over.
Theoretically, iterating
document.linkswill be a bit faster than jQuery’s Sizzle selector library. However:not the way you’re doing it with
filter, which gives jQuery just as much work to do than it would have to do using Sizzle to pick the elements in the first place;document.linkswon’t necessarily give you exactly the same as$('a'), as a-without-href doesn’t appear in thelinkscollection.the direct
$('a.someClass')method will be much faster than even manually iteratingdocument.linksin modern browsers, because that method will just transfer control to the browser’s own implementation ofdocument.querySelectorAll('a.someClass'), which will be much faster than anything your or Sizzle could do sniffing at DOM Nodes yourself.(There is one slightly faster method than
querySelectorAll, which jQuery doesn’t use yet:document.getElementsByClassName('someClass'). Again, it’s only in modern browsers though, and IE8 doesn’t have it where it does havequerySelectorAll. In practice it’s probably not worth bothering about too much asquerySelectorAllis already very fast.)