GetElementsByTagName vs selectNodes efficiency and compatible selectNodes implementation

Source: Internet
Author: User

So I tested it: Copy codeThe Code is as follows: var stringToDom = function (text ){
Var doc;
If (window. ActiveXObject ){
Doc = new ActiveXObject ("MSXML2.DOMDocument ");
Doc.loadXML(text).doc umentElement;
} 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 ("");
}
Document. write ("getElementsByTagName:", new Date ()-d1 );
D1 = new Date ();
Try {
For (var I = 0; I <100000; I ++ ){
C = xmlDoc. selectNodes ("");
}
Document. write ("<br/> selectNodes:", new Date ()-d1 );
} Catch (ex) {document. write ("<br/> error:" + ex )}

SelectNodes is much faster in IE,
This method is not available in FF, but google finds a method and uses XPathEvaluator to implement it. The following is a specific implementation, but the efficiency is not ideal:Copy codeThe 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 [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 shoshould 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 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 -- 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).doc umentElement;
} 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 ("");
}
Document. write ("getElementsByTagName:", new Date ()-d1 );
D1 = new Date ();
Try {
For (var I = 0; I <100000; I ++ ){
C = xmlDoc. selectNodes ("");
}
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 ("");
Document. write ("<br/> getElementsByTagName:", new Date ()-d1 );
D1 = new Date ();
C = xmlDoc. selectNodes ("");
Document. write ("<br/> selectNodes:", new Date ()-d1 );
*/
</Script>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.