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

时间:2024-04-22 13:01:07 

于是就测试了下:


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>
标签:getElementsByTagName,selectNodes
0
投稿

猜你喜欢

  • 儿童编程python入门

    2021-03-12 15:25:06
  • python入门课程第二讲之怎么运行Python

    2023-06-12 11:44:54
  • python查找与排序算法详解(示图+代码)

    2023-08-05 13:27:26
  • Pycharm学习教程(4) Python解释器的相关配置

    2023-12-01 10:11:32
  • 再说淘宝的评价和信用机制

    2008-07-10 12:43:00
  • asp测字符串长度及截取定长字符串汉字的处理

    2007-10-12 13:14:00
  • Flask缓存静态文件的具体方法

    2023-04-12 18:27:43
  • nodejs处理tcp连接的核心流程

    2024-05-03 15:55:40
  • Vue之使用mockjs生成模拟数据案例详解

    2024-05-29 22:24:40
  • python连接池实现示例程序

    2022-04-30 21:59:54
  • Python代理抓取并验证使用多线程实现

    2022-07-10 07:04:19
  • FrontPage XP设计制作网页小技巧八则

    2008-06-04 12:43:00
  • laravel 实现划分admin和home 模块分组

    2024-05-22 10:02:03
  • 用ASP编程实现网络内容快速查找

    2007-09-16 17:56:00
  • mysql事务隔离级别详情

    2024-01-17 13:56:23
  • 跟老齐学Python之Import 模块

    2022-02-02 21:13:34
  • asp如何取回已忘记的密码?

    2010-05-13 16:33:00
  • CSS扫盲(一): padding

    2009-08-04 17:58:00
  • Python3enumrate和range对比及示例详解

    2021-02-05 02:11:47
  • Mysql常用命令行大全

    2009-01-15 16:34:00
  • asp之家 网络编程 m.aspxhome.com