亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2010年02月26日 14:35:37   作者:  
天在csdn上看到有人問(wèn) getElementsByTagName 和 selectNodes誰(shuí)更快 ,這個(gè)還真沒(méi)研究過(guò)。
于是就測(cè)試了下:
復(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下卻沒(méi)有這個(gè)方法,google了下,找了方法,使用XPathEvaluator來(lái)實(shí)現(xiàn),下面是具體實(shí)現(xiàn),不過(guò)效率就不太理想了:
復(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
完整的測(cè)試頁(yè)面:
復(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倒計(jì)時(shí)顯示實(shí)例

    js倒計(jì)時(shí)顯示實(shí)例

    本文分享了js倒計(jì)時(shí)顯示的實(shí)例,需要的朋友可以參考借鑒,下面就跟小編一起來(lái)看看吧
    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)存既不能使用,又不能回收,直到瀏覽器進(jìn)程結(jié)束。下面這篇文章主要給的大家介紹了關(guān)于JavaScript中內(nèi)存泄漏的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • 一篇文章弄懂js中的typeof用法

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

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

    javascript繪制漂亮的心型線效果完整實(shí)例

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

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

    下面小編就為大家?guī)?lái)一篇js添加事件的通用方法推薦。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • Javascript&DHTML基礎(chǔ)知識(shí)

    Javascript&DHTML基礎(chǔ)知識(shí)

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

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

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

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

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

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

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

最新評(píng)論