bcr07548@creighton.edu wrote:
Please provide attribution of quoted material as layed out in the FAQ Notes.
vvvvvvvvvvvvvvvvvvvvvvvvvv[color=blue][color=green][color=darkred]
>> >> > I thought that getElementById() would return null if the ID was
>> >> > invalid.
>> >> Not quite.
>> >>
>> >> <URL:http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-getElBId>
>> > "If no such element exists, this returns null." ;-) It also says that
>> > it doesn't throw an exception.[/color]
>> Read it again, _completely_.[/color]
>
> I assume you mean "if more than one element has an ID attribute with
> that value, what is returned is undefined?"[/color]
So you finally found it.
[color=blue]
> In this example, the element that may or may not be output by the PHP code
> is in an if-statement so either the element with the given ID is generated
> once or not at all so you can be 100% certain that what is returned by
> getElementById() will either be a reference to the object with the
> given ID or null.[/color]
While your reasoning is still flawed (there may be another `if' statement
or that `if' statement may be executed in a loop), I can accept it
provisionally.
[color=blue][color=green]
>> Furthermore, the language does not matter here. It is the repeated DOM
>> object reference retrieval that is unnecessary and makes the former
>> approach ineffcient.[/color]
>
> Right. I even said that in my response[/color]
You did not. And you are still referring to JavaScript only, in order to
be able to maintain your factually false statement of JavaScript being
interpreted source code where repetitions would not matter anyway. It is
not, and they do.
[color=blue]
> but you are missing the point.[/color]
I am not.
[color=blue]
> It's just a simple JavaScript and it runs more or less instantaneously
> on any half-way modern machine.[/color]
So? That does not make any difference regarding efficiency of the code.
[color=blue][color=green][color=darkred]
>> > I know there is a redundant lookup involved in the example I gave but
>> > come on! I wouldn't be surprised if most browsers kept track of id's
>> > and their associated objects in a binary tree or hash map "behind the
>> > scenes" but[/color]
>> But apparently they do not.[/color]
>
> Apparently? I'm not saying that they do (because I haven't looked at
> the browser code) but have you looked at all the code for Mozilla
> either? It doesn't seem so far fetched seeing as a tree is build for
> DOM nodes anyway.[/color]
A (document) tree /is/ build for DOM nodes. That is a matter of fact, and
was not debated. However, it does not have anything to do with your wild
assumption that node lookups are cached somehow, and therefore repeated
lookups of the same node would not matter much regarding runtime
efficiency. They /do/ matter, as tests have proven. So there is good
reason to believe that there is no implementation of such a lookup cache
in the tested user agents, including Gecko-based browsers. You do not
have to look at the source code of any of the tested UAs to come to that
conclusion.
[color=blue][color=green][color=darkred]
>> > even if they don't, calling getElementById() a few extra times is not
>> > going to have a *noticable* effect.[/color]
>> Yes, it is.[/color]
>
> So tell me, how many fractions of a millisecond does a getElementById()
> call take. ;-)[/color]
A single Document::getElementById() call will not make much of a difference.
But _repeated_ lookups of the same item will do and in fact they do. It is
that difference in runtime efficiency that makes competent developers to
avoid developing the habit of doing repeated lookups of the same item in
the first place ...
[color=blue]
> [snipped][/color]
.... but your further statements clearly indicate that you are no such person
(as yet).
PointedEars