I think it's useful to consider things beyond speed.
Unless your users can "really" tell the difference between the different
selector speeds, you might want to consider the overall readability and
maintainability. I am a big believer in "striving" towards Semantic
Markup. This means that lists
With classes it's faster because searching for just a class means
traversing the entire DOM looking for elements with the class. But
with tag.class, jQuery does document.getElementsByTagName('tag') and
only searches those elements for the given class.
I think tag#id works the same ways as tag.cl
css:
faster div.someClass
slower .someClass
id:
faster #someId
slower div#someId
With IDs, if you use a simple search like "#someId", jquery knows to use
getElementById('someId'), which is MUCH faster than searching through every
div on the page and checking each one for an id of 'someId'. With
I believe you can speed it up even further if the tags with classes are
in a div that has an ID.
$("#divID tag.class")
This way the dom traversal is limited to only that section of the document.
http://www.learningjquery.com/2006/12/quick-tip-optimizing-dom-traversal
-Marshall
spinnach wr
if the elements you search for are all of the same type (eg. div), it's
much faster to include the tag, because without the tag jquery would
have to search through all elements on the page to find the elements
with the corresponding class, instead of just searching through the divs..
dennis.
Erik I was under the impression that the exact opposite was true for
searches by class: that including the tag slows it down.
I may be mistaken, you'd have to check the list archives or perhaps check
the script, I may do the same tomorrow.
rob
On 4/2/07, Erik Beeson <[EMAIL PROTECTED]> wrote:
ID is found by using document.getElementById(), which is probably
about as fast a DOM operation as can be had. Use ID for sure.
Also, if I'm not mistaken, it helps class searches a lot if you also
include the tag that you're looking for: $('div.someClass') instead of
just $('.someClass').
--Eri
Geoff
If they are unique and speed is important then id is the only way to go.
Classes are for when there are several instances of the same *type* of
thing.
rob
On 4/2/07, Geoffrey Knutzen <[EMAIL PROTECTED]> wrote:
I have the option of naming a few containers using either classes or ids.
It
8 matches
Mail list logo