SoFunction
Updated on 2025-04-09

getElementsByTagName vs selectNodes efficiency and compatible selectNodes implementation

So I tested it:
Copy the codeThe code is as follows:

var stringToDom=function(text) {
var doc;
if() {
doc = new ActiveXObject("");
(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=("a");
}
("getElementsByTagName: ",new Date()-d1);
d1=new Date();
try{
for(var i=0;i<100000;i++){
c=("a");
}
("<br/>selectNodes: ",new Date()-d1);
}catch(ex){("<br/>error:"+ex)}

SelectNodes is much faster under IE.
It can be FF but there is no method. I went to Google, found the method, and used XPathEvaluator to implement it. The following is the specific implementation, but the efficiency is not ideal:
Copy the codeThe code is as follows:

if (!) {
(function(){
var oEvaluator=new XPathEvaluator(),oResult;
= function(sXPath) {
oResult = (sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var aNodes = [];
if (oResult != null) {
var oElement = ();
while (oElement) {
aNodes[]=oElement;
oElement = ();
}
}
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:///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
Complete test page:
Copy the codeThe code is as follows:

<!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 -- /sohighthesky
*content: selectNodes vs getElementsByTagName
*/
if (!) {
(function(){
var oEvaluator=new XPathEvaluator(),oResult;
= function(sXPath) {
oResult = (sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var aNodes = [];
if (oResult != null) {
var oElement = ();
while (oElement) {
aNodes[]=oElement;
oElement = ();
}
}
return aNodes;
}
= function(sXPath) {
oResult = (sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
// FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
return oResult==null?null:;
}
})()
}
var stringToDom=function(text) {
var doc;
if() {
doc = new ActiveXObject("");
(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=("a");
}
("getElementsByTagName: ",new Date()-d1);
d1=new Date();
try{
for(var i=0;i<100000;i++){
c=("a");
}
("<br/>selectNodes: ",new Date()-d1);
}catch(ex){("<br/>error:"+ex)}
/*
var n=("body/a"),doc=("body");//alert([0].nodeValue)
for(var i=0;i<10000;i++){
((true))
}
d1=new Date();
c=("a");
("<br/>getElementsByTagName: ",new Date()-d1);
d1=new Date();
c=("a");
("<br/>selectNodes: ",new Date()-d1);
*/
</script>
</html>