In other words, which line is easier for jQuery to perform: $("#id3") or $("#id1 #id2 #id3"), where id1,id2 and id3 are nested divs' ids. Are there any rules for writing fast jQuery selectors?
views:
32answers:
1
+2
A:
$('#id3') is quicker for jQuery to do, as it detects the special case of a single # selector and redirects to document.getElementById(). It's also clearly simpler; you should use a single ID selector unless you really do need to check that the element with id="id3" is inside the element with id="id2".
Note that for most other standard CSS selectors like this (ie not the questionable Sizzle-specific selectors like :first), jQuery will try to use the querySelectorAll() method of modern browsers, so the speed of Sizzle matters less than the browser's own performance, making it a different question.
bobince
2010-10-16 20:43:53