欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

getElementsByTagName vs selectNodes效率 及兼容的selectNodes實現(xiàn)

 更新時間:2010年02月26日 14:35:37   作者:  
天在csdn上看到有人問 getElementsByTagName 和 selectNodes誰更快 ,這個還真沒研究過。
于是就測試了下:
復(fù)制代碼 代碼如下:

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來實現(xiàn),下面是具體實現(xiàn),不過效率就不太理想了:
復(fù)制代碼 代碼如下:

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
完整的測試頁面:
復(fù)制代碼 代碼如下:

<!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>

相關(guān)文章

  • js倒計時顯示實例

    js倒計時顯示實例

    本文分享了js倒計時顯示的實例,需要的朋友可以參考借鑒,下面就跟小編一起來看看吧
    2016-12-12
  • (轉(zhuǎn)載)JavaScript中匿名函數(shù),函數(shù)直接量和閉包

    (轉(zhuǎn)載)JavaScript中匿名函數(shù),函數(shù)直接量和閉包

    (轉(zhuǎn)載)JavaScript中匿名函數(shù),函數(shù)直接量和閉包...
    2007-05-05
  • JavaScript中內(nèi)存泄漏的介紹與教程(推薦)

    JavaScript中內(nèi)存泄漏的介紹與教程(推薦)

    內(nèi)存泄露是指一塊被分配的內(nèi)存既不能使用,又不能回收,直到瀏覽器進程結(jié)束。下面這篇文章主要給的大家介紹了關(guān)于JavaScript中內(nèi)存泄漏的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • 一篇文章弄懂js中的typeof用法

    一篇文章弄懂js中的typeof用法

    這篇文章主要給大家介紹了關(guān)于js中typeof用法的相關(guān)資料,typeof運算符把類型信息當(dāng)作字符串返回,包括有大家常有變量類型,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • javascript繪制漂亮的心型線效果完整實例

    javascript繪制漂亮的心型線效果完整實例

    這篇文章主要介紹了javascript繪制漂亮的心型線效果實現(xiàn)方法,結(jié)合完整實例形式分析了JavaScript圖形繪制的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-02-02
  • js添加事件的通用方法推薦

    js添加事件的通用方法推薦

    下面小編就為大家?guī)硪黄猨s添加事件的通用方法推薦。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Javascript&DHTML基礎(chǔ)知識

    Javascript&DHTML基礎(chǔ)知識

    首先請下載JScript.chm這本手冊,無論新手老手,有一本手冊是免不了的,特別是對于新手,如果你沒有空翻犀牛書,那么這本手冊將是你了解這門語言的首選。下面所講的大多數(shù),手冊上可以沒有提及,或提及很少的內(nèi)容。
    2008-07-07
  • JavaScript代碼復(fù)用模式實例分析

    JavaScript代碼復(fù)用模式實例分析

    任何編程都提出代碼復(fù)用,否則話每次開發(fā)一個新程序或者寫一個新功能都要全新編寫的話,效率太差了,接下來我們將針對代碼復(fù)用來進行討論,需要的朋友可以參考下
    2012-12-12
  • Webpack執(zhí)行命令參數(shù)詳解

    Webpack執(zhí)行命令參數(shù)詳解

    本篇文章主要介紹了Webpack執(zhí)行命令參數(shù)詳解 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 詳解嵌套命名空間在TypeScript中如何應(yīng)用

    詳解嵌套命名空間在TypeScript中如何應(yīng)用

    命名空間是TypeScript中非常有用的概念,可以幫助我們組織和管理代碼,避免命名沖突,下面小編就來和大家聊聊嵌套命名空間在TypeScript中是如何應(yīng)用的吧
    2023-06-06

最新評論