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.class. It does
document.getElementsByTagName('tag') and then searches those elements
for one with the given id. Though maybe tag#id is treated the same as
$('#id').filter('tag'), in which case the difference is speed would
probably be negligible.

This comes up every other month or so. There should really be a FAQ about it.

--Erik


On 4/2/07, Matt Stith <[EMAIL PROTECTED]> wrote:
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 classes
its the exact opposite, though im not quite sure why :P


On 4/2/07, spinnach <[EMAIL PROTECTED]> 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