Home  >  Article  >  Web Front-end  >  getElementsByTagName vs selectNodes efficiency and compatible selectNodes implementation_javascript skills

getElementsByTagName vs selectNodes efficiency and compatible selectNodes implementation_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:33:531146browse

So I tested it:

Copy the code The code is as follows:

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("ab"),
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("
selectNodes: ",new Date()-d1);
}catch(ex){document.write("< ;br/>error:" ex)}

It is much faster to select Nodes under IE.
It can be done under FF but there is no such method. I googled it and found a method, using XPathEvaluator To implement, the following is the specific implementation, but the efficiency is not ideal:
Copy the code The code is as follows:

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
Full test page:
Copy code The code is as follows:




selectNodes&getElementsByTagName








Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn