I need something faster than this: <div id="people"> <input type="checkbox" class="checkbox"... x 1000 </div> $(''people'').getElementsByClassName(''checkbox'').each(function(a) { $(a).checked = true; }); TIA --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Eben Elmore wrote:> I need something faster than this: > > <div id="people"> > <input type="checkbox" class="checkbox"... x 1000 > </div> > > $(''people'').getElementsByClassName(''checkbox'').each(function(a) { > $(a).checked = true; });This would be faster as it would only search input boxes. $$("#people input").findAll(function(e) { return e.className == "checkbox" }); Of course this assumes the class name is just "checkbox" and nothing more. If you might have other class names in the same element you can use this which is more clear but slightly slower because of the regexp test if the == method test fails. $$(''#people input.checkbox'') If you want all checkboxes under #people (i.e. regardless of classname) you could do: $$("#people input[type=checkbox]") This gives you the same performance as the first version but you can have any other class name without concern. The only important aspect is that all checkboxes will be considered not just certain checkboxes. If you are running this query often on the same page you could pre-search when the page when loaded, then use your pre-loaded data structure to process the elements. Eric --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you Eric!> If you are running this query often on the same page you > could pre-search when the page when loaded, then use > your pre-loaded data structure to process the elements.Ahh, so I could do: var arPeople = $$("#people input[type=checkbox]"); .... arPeople.each(function(... Am I on the right track? Thanks! Daniel -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Eric Anderson Sent: Monday, February 12, 2007 1:07 PM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Performance tune my checkbox selector Daniel Eben Elmore wrote:> I need something faster than this: > > <div id="people"> > <input type="checkbox" class="checkbox"... x 1000 > </div> > > $(''people'').getElementsByClassName(''checkbox'').each(function(a) { > $(a).checked = true; });This would be faster as it would only search input boxes. $$("#people input").findAll(function(e) { return e.className == "checkbox" }); Of course this assumes the class name is just "checkbox" and nothing more. If you might have other class names in the same element you can use this which is more clear but slightly slower because of the regexp test if the == method test fails. $$(''#people input.checkbox'') If you want all checkboxes under #people (i.e. regardless of classname) you could do: $$("#people input[type=checkbox]") This gives you the same performance as the first version but you can have any other class name without concern. The only important aspect is that all checkboxes will be considered not just certain checkboxes. If you are running this query often on the same page you could pre-search when the page when loaded, then use your pre-loaded data structure to process the elements. Eric --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Additionally, the $(a).checked = true; could be changed to a.checked = true; since "a" is already the DOM element. Probably not a huge gain there. I might get flamed for this, but you might also try a non-Prototype approach to save the overhead of extending elements, extra function calls, etc.. If the browser and Prototype are taking advantage of XPath it may be possible that this is slower, but I doubt it. var els = $(''people'').getElementsByTagName(''input''); for( var i=0; i < els.length; i++){ if(els[i].type == ''checkbox''){ els[i].checked = true; } } If it really matters, run some tests! Colin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Eben Elmore wrote:> var arPeople = $$("#people input[type=checkbox]"); > > .... > > arPeople.each(function(... > > Am I on the right track?Yes. Here is how I would write it: $$("#people input[type=checkbox]").each(function(e){e.checked = true}) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Eric Anderson a écrit :> $$("#people input[type=checkbox]").each(function(e){e.checked = true})And that''s about as fast as you''ll get with the Prototype way. Note there is no lexical closure cost here. The only significantly faster way, if you really need it, would be to store the $$ result in a variable, then do a vanilla loop over the array, saving the anonymous function call every time. -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you Christophe, Colin, Eric. Very helpful. -Daniel -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Christophe Porteneuve Sent: Monday, February 12, 2007 3:49 PM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Performance tune my checkbox selector Eric Anderson a écrit :> $$("#people input[type=checkbox]").each(function(e){e.checked = true})And that''s about as fast as you''ll get with the Prototype way. Note there is no lexical closure cost here. The only significantly faster way, if you really need it, would be to store the $$ result in a variable, then do a vanilla loop over the array, saving the anonymous function call every time. -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---