views:

566

answers:

4

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?

+2  A: 

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.

annakata
a use case could be where you want to set the 'target' property for all hrefs in a standards compliant xhtml document
KooiInc
Another use-case may be found here http://stackoverflow.com/questions/435429/browser-agnostic-c-dom-interface/483736#483736
Motti
@Nicon - then you still don't need "*" you need "*[href]" (or more probably just "a"). @motti - I'm not seeing what you mean
annakata
Say I have C++ code that uses MSHTML::IHTMLElement.get_all and I want it to work on Firefox, I may create a COM class that implements IHTMLElement and holds an inner nsIDOMNSHTMLElement. In that case how do I implement get_all ?
Motti
not really a JS question at that point is it? :) I think someone familiar with the codebase you're talking about would have to answer that from a C++ perspective
annakata
A: 

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.

meouw
+5  A: 

For 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.

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.

Kent Fredric
Funny thing, I was just listening to this lecture when you posted this.
Motti
wow - that is *strange*
annakata
Not really it was on reddit.
Motti
A: 

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.

Mr. Shiny and New