jeffrey.daily-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-02 16:53 UTC
questions regarding the use of recursivelyCollect
Some quick background: I''m developing a portlet to be deployed in Liferay 4.2.1. They use prototype as their javascript framework. One function that is used in a couple of places is getElementsByClassName. I have problems with the use of that function because of its use of getElementsByTag("*"). I even altered getElementsByClassName to take a third argument so as to replace the call as getElementsByTag(tagName || "*"). That only offered a moderate performance increase. Performance is an issue because my portlet is essentially a 35x900 table, which is unavoidable. I am now considering walking the DOM, since getElementsByClassName was being used to only fetch the 0''th element of the returned array. If anyone knows of a quick way to nab the first matching element, please tell me! I am new to prototype, but it seems pretty straightforward to use. A function like "getFirstElementByClassName" would be spectacular in this case, especially if it didn''t retrieve all descendent nodes in the process. I thought about implementing it myself using prototype''s up/down/ previous/next functions, but as far as I can tell reading the source, those functions also get all descendents via recursivelyCollect(previousSibling/nextSibling). Am I mistaken about how recursivelyCollect works in the above cases? Will recursivelyCollect(''nextSibling'') dive through all descendents, or just those one level deep? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Feb 3, 2:53 am, "jeffrey.da...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <jeffrey.da...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Some quick background: > I''m developing a portlet to be deployed in Liferay 4.2.1. They use > prototype as their javascript framework. One function that is used in > a couple of places is getElementsByClassName. I have problems with > the use of that function because of its use of getElementsByTag("*"). > I even altered getElementsByClassName to take a third argument so as > to replace the call as getElementsByTag(tagName || "*"). That only > offered a moderate performance increase. Performance is an issue > because my portlet is essentially a 35x900 table, which is > unavoidable.Which version of Prototype? getElementsByClassName was notoriously slow in 1.4, however 1.5 offers an XPath fork which is much faster than DOM methods. Browsers that don''t support XPath still get a slow method, the following is 2 to 4 times faster than the non-XPath fork in Prototype (depending on browser) but is slower than the XPath fork: /* return first element that matches a particular class name * className - required, a valid class name. * parentElement - optional, a DOM element reference. * If missing, document is used. * tagName - optional, restricts search to only elements * with that tag name. */ function getFirstElementByClassName(className, parentElement, tagName) { parentElement = parentElement || document; var nodes = parentElement.getElementsByTagName(tagName || ''*''); var i=0; var classRe = new RegExp("(^|\\s)" + className + "(\\s|$)"); while(node = nodes[i++]) { if (classRe.test(node.className)){ return node; } } } -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---