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 wrote:

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.

Rob Desbois wrote:
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] <mailto:[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').

    --Erik


    On 4/2/07, Geoffrey Knutzen <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>> wrote:
     >
     >
     >
     >
     > I have the option of naming a few containers using either classes
    or ids. It
     > is assured that there will only be 1 instance of one of these
    containers on
     > any page.
     >
     > Is it better to name them with a class or an id?
     >
     > Is there any speed benefit from using an id?
     >
     >
     >
     > These containers will be accessed rather frequently, so speed
    would make a
     > difference
     >
     >
     >
     > Any thoughts?
     >
     >
     >
     > Thanks
     >
     > -Geoff
     >
     >




--
Rob Desbois
Eml: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
Tel: 01452 760631
Mob: 07946 705987
"There's a whale there's a whale there's a whale fish" he cried, and the whale was in full view.
...Then ooh welcome. Ahhh. Ooh mug welcome.



Reply via email to