Re: What's faster, saving an HTML DOM node as a variable, or using getElementById?
- From: ctman770@xxxxxxxxx
- Date: 14 Sep 2006 16:27:27 -0700
Actually,
One question that pops to my head now is that if I do end up using an
array to store id locations, if I make innerHTML replacements or
additions to other tags either before or after the div, do the
locations all go bad? In that case, it might be better to just use
getElementById all the time.
Thanks,
Clarence
RobG wrote:
ctman770@xxxxxxxxx wrote:
Hi Everyone,
Is it faster to save the precise location of an html dom node into a
variable in js, or to use getElementById everytime you need to access
the node?
That depends on how often you intend to access the node. If it is less
than a few hundred times in a tight loop, it likely makes no noticeable
difference.
I'd suggest testing it to find out where the tipping point is, then
deciding if you are ever likely to approach it.
I want to make my application as fast as possible. I have about 10-20
id tags that need to be accessed and modified from time to time. Would
the jvm perform slowly if I stored all of the dom node strings
"document.node.child...." into a huge js array?
I don't think an array of 20 items is huge. 20,000 is large.
Alternatively, would it also be slow if I had to use getElementById()
everytime I needed to access the node?
"Slow" is relative. If you are accessing the nodes infrequently, then
it likely makes zero difference to the perceived speed.
It is difficult to recommend a particular approach without knowing more
about the use to which it will be put, but generally I prefer to store
references in an array. I prefer to identify the nodes to store using
some strategy other than say a sequence of IDs (el-0, el-1, etc.), or a
list provided as an array from some other source.
To me it's much easier to wrap the elements in question in a div and
pass its ID to a function, then use getElementsByTagName or similar to
find the ndoes I want (maybe further distinguished by CSS class name)
and store references to them. For some nodes you can use a common name
attribute and getElementsByName, but that only suits a small sub-set of
elements.
--
Rob
.
- Follow-Ups:
- References:
- Prev by Date: Re: Javascript on steroids!
- Next by Date: Re: put XML document in json
- Previous by thread: Re: What's faster, saving an HTML DOM node as a variable, or using getElementById?
- Next by thread: Re: What's faster, saving an HTML DOM node as a variable, or using getElementById?
- Index(es):
Relevant Pages
|