Let me start out by saying that I'm not a JavaScript developer so this question may be rather basic.
让我首先说我不是JavaScript开发人员所以这个问题可能是相当基本的。
When simulating IE's nonstandard all
property I'm using getElementsByTagName("*")
, is there a significant performance difference between both methods?
当模拟IE的非标准所有属性我正在使用getElementsByTagName(“*”)时,两种方法之间是否存在显着的性能差异?
4 个解决方案
#1
Essentially there should be no noticeable performance hit, and the use of document.all
is unacceptable anyway.
基本上应该没有明显的性能损失,并且无论如何使用document.all都是不可接受的。
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.
然而,有一个问题是,为什么你有兴趣收集一组每个元素呢?我想不出那个副手的单一用例,而另一种方式无法更好地处理。
#2
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.
对于兴趣,您可能会发现John Resig的这个讲座很有趣。当使用像你这样的dom方法时,它与新手和有经验的用户相关。
It discusses many lovely caveats of dom methods in many browsers.
它在许多浏览器中讨论了许多可靠的dom方法警告。
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:
一个这样的,是的getElementsByTagName(“*”)将返回IE5没有元素,并做奇怪的事情与IE7下对象的getElementsByTagName +(“*”),并根据对话,它使这样的:
<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.
我不知道哪个javascript库绕过了这个漏洞,但是你真的应该使用它来避免跨浏览器的头痛。
#3
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.
不是性能影响,但值得注意:从getElementsByTagName返回的nodeList是实时的。如果你操纵DOM,列表也会改变以反映这一点。
#4
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.
不同浏览器和不同版本的浏览器具有不同的性能特征。如果您正在操作大型DOM,则应对您关注的浏览器进行基准测试。考虑人们发布的Javascript库基准测试。它们展示了不同浏览器的性能差异。因此,如果不知道您使用的是哪种浏览器,问题就无法解决。但是,对于大多数机器上的大多数人来说,你也应该过度优化可能有效地占用零时间的东西。
#1
Essentially there should be no noticeable performance hit, and the use of document.all
is unacceptable anyway.
基本上应该没有明显的性能损失,并且无论如何使用document.all都是不可接受的。
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.
然而,有一个问题是,为什么你有兴趣收集一组每个元素呢?我想不出那个副手的单一用例,而另一种方式无法更好地处理。
#2
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.
对于兴趣,您可能会发现John Resig的这个讲座很有趣。当使用像你这样的dom方法时,它与新手和有经验的用户相关。
It discusses many lovely caveats of dom methods in many browsers.
它在许多浏览器中讨论了许多可靠的dom方法警告。
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:
一个这样的,是的getElementsByTagName(“*”)将返回IE5没有元素,并做奇怪的事情与IE7下对象的getElementsByTagName +(“*”),并根据对话,它使这样的:
<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.
我不知道哪个javascript库绕过了这个漏洞,但是你真的应该使用它来避免跨浏览器的头痛。
#3
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.
不是性能影响,但值得注意:从getElementsByTagName返回的nodeList是实时的。如果你操纵DOM,列表也会改变以反映这一点。
#4
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.
不同浏览器和不同版本的浏览器具有不同的性能特征。如果您正在操作大型DOM,则应对您关注的浏览器进行基准测试。考虑人们发布的Javascript库基准测试。它们展示了不同浏览器的性能差异。因此,如果不知道您使用的是哪种浏览器,问题就无法解决。但是,对于大多数机器上的大多数人来说,你也应该过度优化可能有效地占用零时间的东西。