Moss Moss - 1 year ago 66
jQuery Question

How do the performance characteristics of jQuery selectors differ from those of CSS selectors?

I came across Google's Page Speed add-on for Firebug yesterday. The page about using efficient CSS selectors said to not use overqualified selectors, i.e. use

instead of
. I thought the latter would be faster but Google's saying otherwise, and who am I to go against that?

So that got me wondering if the same applied to jQuery selectors. This page I found the link to on SO says I should use
, which is what I was doing all along, since I thought that things would speed up by limiting the selector to match
elements only. But is it really better than writing
like Google's saying for CSS selectors, or do CSS versus jQuery element matching work in different ways and I should stick with

Answer Source

$("#foo") is better than $("div#foo")

Since id is unique in the document you don't have to prefix it with a tag name.

Here is a nice link

jQuery Performance Rules

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download