Re: which is faster target.getElementsByTagName or target.firstChild / nextSibling

Chris <[email protected]> Fri, 07 Apr 2006 00:14:09 -0400
Newsgroups gmane.comp.web.dom.wdf
Message-ID <[email protected]>
I've never run a time test on these 2 approaches, but in my experience 
writing the CSS selector portion of JsEventDefs, which wins depends on 
what you're trying to do.

.getElementsByTagName works great if you're trying to get a list of tags 
regardless of parent/child relationship - sort of like a simple 
selector, 'li' for example. One reason is because you offload looping 
through the DOM to the browser, but another is because you don't need to 
filter for TextNodes getting mixed in with your Elements.

.firstChild/.nextSibling work better if parent/child is important - 'ul 
li ul li' for example. The filtering operation you need to run on the 
results of .getElementsByTagName is more expensive than the time you 
saved by getting all those tags in one batch. In many cases, much, much 
more expensive.

In the case of JsEventDefs, for the next version I intend to use the 
Strategy design pattern to use different methods for fetching tags, 
depending on what the selector looks like. The current version uses the 
.getElementsByTagName approach alone, and suffers on selectors like 
'div.tree li li li'.

-Chris


Moran Ben-David wrote:
> You could try calling these functions in a loop 1000 times and measuring the
> time to execute the loop.  This way you have a test that can you can run on
> each browser you intend to support.
>   
>> -----Original Message-----
>> From: [email protected] [mailto:[email protected]] On Behalf
>> Of becs027
>> I'm doing an onload script to add rollover behaviour to list items
>> (dropdown menu style) for IE, and want to check which of two ways is
>> faster.
>>
>> ------------------------------------------------
>>
>> Option 1 - add the behaviour using target.getElementsByTagName:
>> Option 2 - add the behaviour with a while loop using firstChild,
>> nextSibling:
>>
>> Thanks :)
>> Rebecca
>>     



Unsubscribe
[email protected]

List info
http://www.quirksmode.org/dom/list.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wdf-dom/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/