Let me start out by saying that I'm not a JavaScript developer so this question may be rather basic.
When simulating IE's nonstandard all
property I'm using getElementsByTagName("*")
, is there a significant performance difference between both methods?
Let me start out by saying that I'm not a JavaScript developer so this question may be rather basic.
When simulating IE's nonstandard all
property I'm using getElementsByTagName("*")
, is there a significant performance difference between both methods?
4 Answers
Reset to default 7For Interest, you may find this lecture by John Resig interesting. Its relevant to new and experienced users alike when using dom methods like you are.
- http://ejohn/blog/the-dom-is-a-mess/
It discusses many lovely caveats of dom methods in many browsers.
One such, is that getElementsByTagName(“*”)
will return no elements in IE5, and does weird things with Objects + getElementsByTagName("*")
under IE7, and according to the talk, it makes this:
<a id="length"></a>
Perform as if somebody had done:
var a = getElementsByTagName("a");
a.length = ""; # This overrides the arrays length attribute :/
So that you can't iterate the array.
I don't know which javascript libraries circumvent this flaw, but you really should use one to avoid cross-browser headaches.
Essentially there should be no noticeable performance hit, and the use of document.all
is unacceptable anyway.
There is however a question as to why you would be interested in collecting a set of every element anyway? I can't think of a single use case for that off-hand that couldn't be handled better another way.
Not really a performance implication but worth noting: The nodeList returned from getElementsByTagName is live. If you manipulate the DOM the list will also change to reflect this.
Different browsers and different versions of browsers have different performance characteristics. If you are manipulating large DOMs you should benchmark on the browsers you care about. Consider the Javascript library benchmarks people post. They demonstrate how much the performance can vary across the different browsers. So the question isn't really answerable without knowing what browser you are using. However you should also be wary of over-optimizing something that may effectively take zero time for most people on most machines.