getElementsByTagName vs selectNodes效率 及兼容的selectNodes实现

 更新时间:2010年02月26日 14:35:37   作者:  
天在csdn上看到有人问 getElementsByTagName 和 selectNodes谁更快 ,这个还真没研究过。
于是就测试了下:
复制代码 代码如下:

var stringToDom=function(text) {
var doc;
if(window.ActiveXObject) {
doc = new ActiveXObject("MSXML2.DOMDocument");
doc.loadXML(text).documentElement;
} else {
doc = (new DOMParser()).parseFromString(text,"text/xml");
}
return doc;
}
var xmlDoc=stringToDom("<body><a href='a'>a</a><a href='b'>b</a></body>"),
c,
d1=new Date();
for(var i=0;i<100000;i++){
c=xmlDoc.getElementsByTagName("a");
}
document.write("getElementsByTagName: ",new Date()-d1);
d1=new Date();
try{
for(var i=0;i<100000;i++){
c=xmlDoc.selectNodes("a");
}
document.write("<br/>selectNodes: ",new Date()-d1);
}catch(ex){document.write("<br/>error:"+ex)}

在IE下selectNodes还是快多了,
可以FF下却没有这个方法,google了下,找了方法,使用XPathEvaluator来实现,下面是具体实现,不过效率就不太理想了:
复制代码 代码如下:

if (!window.ActiveXObject) {
(function(){
var oEvaluator=new XPathEvaluator(),oResult;
XMLDocument.prototype.selectNodes = function(sXPath) {
oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var aNodes = [];
if (oResult != null) {
var oElement = oResult.iterateNext();
while (oElement) {
aNodes[aNodes.length]=oElement;
oElement = oResult.iterateNext();
}
}
return aNodes;
}
})()
}

evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result);
Returns an XPathResult based on an XPath expression and other given parameters.
xpathExpression is a string representing the XPath to be evaluated.
contextNode specifies the context node for the query (see the [http://www.w3.org/TR/xpath XPath specification). It's common to pass document as the context node.
namespaceResolver is a function that will be passed any namespace prefixes and should return a string representing the namespace URI associated with that prefix. It will be used to resolve prefixes within the XPath itself, so that they can be matched with the document. null is common for HTML documents or when no namespace prefixes are used.
resultType is an integer that corresponds to the type of result XPathResult to return. Use named constant properties, such as XPathResult.ANY_TYPE, of the XPathResult constructor, which correspond to integers from 0 to 9.
result is an existing XPathResult to use for the results. null is the most common and will create a new XPathResult
完整的测试页面:
复制代码 代码如下:

<!doctype HTML>
<html>
<head>
<title>selectNodes&getElementsByTagName</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="sohighthesky"/>
<meta name="Keywords" content="selectNodes vs getElementsByTagName"/>
</head>
<body>
</body>
<script type="text/javascript">
/*
*author:sohighthesky -- http://www.cnblogs.com/sohighthesky
*content: selectNodes vs getElementsByTagName
*/
if (!window.ActiveXObject) {
(function(){
var oEvaluator=new XPathEvaluator(),oResult;
XMLDocument.prototype.selectNodes = function(sXPath) {
oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var aNodes = [];
if (oResult != null) {
var oElement = oResult.iterateNext();
while (oElement) {
aNodes[aNodes.length]=oElement;
oElement = oResult.iterateNext();
}
}
return aNodes;
}
XMLDocument.prototype.selectSingleNode = function(sXPath) {
oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
// FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
return oResult==null?null:oResult.singleNodeValue;
}
})()
}
var stringToDom=function(text) {
var doc;
if(window.ActiveXObject) {
doc = new ActiveXObject("MSXML2.DOMDocument");
doc.loadXML(text).documentElement;
} else {
doc = (new DOMParser()).parseFromString(text,"text/xml");
}
return doc;
}
var xmlDoc=stringToDom("<body><a href='a'>a</a><a href='b'>b</a></body>"),
c,
d1=new Date();
for(var i=0;i<100000;i++){
c=xmlDoc.getElementsByTagName("a");
}
document.write("getElementsByTagName: ",new Date()-d1);
d1=new Date();
try{
for(var i=0;i<100000;i++){
c=xmlDoc.selectNodes("a");
}
document.write("<br/>selectNodes: ",new Date()-d1);
}catch(ex){document.write("<br/>error:"+ex)}
/*
var n=xmlDoc.selectSingleNode("body/a"),doc=xmlDoc.selectSingleNode("body");//alert(n.childNodes[0].nodeValue)
for(var i=0;i<10000;i++){
doc.appendChild(n.cloneNode(true))
}
d1=new Date();
c=xmlDoc.getElementsByTagName("a");
document.write("<br/>getElementsByTagName: ",new Date()-d1);
d1=new Date();
c=xmlDoc.selectNodes("a");
document.write("<br/>selectNodes: ",new Date()-d1);
*/
</script>
</html>

相关文章

  • JSP跨iframe如何传递参数实现代码

    JSP跨iframe如何传递参数实现代码

    表单与操作页面分离,按钮按下,click 或者onclick事件触发,传递一个唯一性的参数至子页面JSP,感兴趣的朋友可以了解下
    2013-09-09
  • javascript基本包装类型介绍

    javascript基本包装类型介绍

    这篇文章主要介绍了javascript的基本包装类型,JS为了便于操作基本类型,提供了3个特殊的引用类型:Boolean/Number和String,需要的朋友可以参考下
    2015-04-04
  • 浅谈Javascript中的12种DOM节点类型

    浅谈Javascript中的12种DOM节点类型

    DOM是javascript操作网页的接口,全称为文档对象模型(Document Object Model)。本文将主要说明DOM节点类型,有需要的可以参考借鉴。
    2016-08-08
  • JavaScript标准对象_动力节点Java学院整理

    JavaScript标准对象_动力节点Java学院整理

    这篇文章主要为大家详细介绍了JavaScript标准对象的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • js闭包的9个使用场景

    js闭包的9个使用场景

    这篇文章主要介绍了js 闭包的9个使用场景,帮助大家更好的理解和学习JavaScript 闭包的使用,感兴趣的朋友可以了解下
    2020-12-12
  • 小程序最新获取用户昵称和头像的方法总结

    小程序最新获取用户昵称和头像的方法总结

    这篇文章主要介绍了小程序最新获取用户昵称和头像的方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • next.js初始化参数设置getServerSideProps应用学习

    next.js初始化参数设置getServerSideProps应用学习

    这篇文章主要为大家介绍了next.js初始化参数设置getServerSideProps的应用示例学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • 小程序scroll-view组件实现滚动的示例代码

    小程序scroll-view组件实现滚动的示例代码

    这篇文章主要介绍了小程序scroll-view组件实现滚动的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • JavaScript获取文本框内选中文本的方法

    JavaScript获取文本框内选中文本的方法

    这篇文章主要介绍了JavaScript获取文本框内选中文本的方法,实例分析了javascript通过onclick获取文本框选中文本的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • bootstrap组件之导航组件使用方法

    bootstrap组件之导航组件使用方法

    Bootstrap 中的导航组件都依赖同一个 .nav 类和ul,状态类也是共用的。改变修饰类可以改变样式。接下来通过本文给大家介绍bootstrap 导航组件使用方法,一起看看吧
    2017-01-01

最新评论