最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What are the performance implications of getElementsByTagName("*")? - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Dec 11, 2017 at 18:01 Motti asked Feb 3, 2009 at 8:48 MottiMotti 115k56 gold badges194 silver badges274 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

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.

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

发布评论

评论列表(0)

  1. 暂无评论