I think comparing two strings would be faster than deciding whether a string begins with another string. Also, doing one selection would almost always be faster than doing 3, so I'd say that the first piece of code from the OP would be faster. It should only require one loop through the DOM tree.
I think the solution that used the "each" would be a little bit slower because there would be some overhead from using the "each" function, rather than simply traversing the jQuery array with .hide(). Now, I say all this from the perspective of a Computer Science major. I've not looked at the jQuery source code for this, so I could be wrong about the way it does this. Of course for a small number of elements (and yes, 50 is still small), it shouldn't make a bit difference (though another consideration is the size of the JS, in which case the first piece of code from the OP is definitely better). On Jan 5, 10:53 am, MorningZ <morni...@gmail.com> wrote: > And another way: > > $("div[id^='toto']").hide(); > > although i couldn't see one or the other or even the other being much > faster considering that the selector would have to go through all the > elements to see if *any* element on the page either (1) has the > specified class or (2) has the specified id > > but i'm no DOM expert, just seems to make sense :-/ > > On Jan 5, 10:50 am, Christof Donat <cdo...@gmx.de> wrote: > > > Hi, > > > > And try something like: > > > > var toto = []; > > > $('.toto').each(function(){ > > > var elem = $(this); > > > toto[elem.attr('ref')] = elem; > > > } > > > > Then, to hide the "toto" div with ref="1" or ref="2", just call: > > > > toto[1].hide(); toto[2].hide(); > > > Why not use this: > > > var toto = $('.toto'); > > toto[1].hide(); toto[2].hide(); > > > That should be the same. > > > Appart from that I think, that Jean Babtiste wanted to hide all elements at > > the same time. I'd expect his first ( $('.toto').hide() ) version to be > > usually faster, but I'm not shure about that. > > > Christof