Opened 14 years ago
Closed 13 years ago
#7261 closed Bug (invalid)
node.js -> getPosition performance poor
Reported by: | campaign | Owned by: | |
---|---|---|---|
Priority: | Normal | Milestone: | |
Component: | Performance | Version: | 3.0 |
Keywords: | HasPatch? | Cc: |
Description
not use getAddress getIndex methods performance poor
my code
getPosition : function ( nodeA, nodeB ) {
如果两个节点是同一个节点 if ( nodeA === nodeB ) {
DOMUtils.POSITION_IDENTICAL return 0;
}
if ( nodeA.compareDocumentPosition ) {
return nodeA.compareDocumentPosition(nodeB);
}
var node,
parentsA = [], parentsB = [], body = nodeA.ownerDocument.body;
node = nodeA; while ( node = node.parentNode ) {
如果nodeB是nodeA的祖先节点 if ( node === nodeB ) {
DOMUtils.POSITION_IS_CONTAINED + DOMUtils.POSITION_FOLLOWING return 10;
} if ( node === body ) break; parentsA.push( node );
}
node = nodeB; while ( node = node.parentNode ) {
如果nodeA是nodeB的祖先节点 if ( node === nodeA ) {
DOMUtils.POSITION_CONTAINS + DOMUtils.POSITION_PRECEDING return 20;
} if ( node === body ) break; parentsB.push( node );
}
parentsA = parentsA.reverse(); parentsA.push(nodeA); parentsB = parentsB.reverse(); parentsB.push(nodeB); 不会出现ai 或者 bi 取不到的情况 for ( var i=0,ai,bi;ai=parentsA[i],bi=parentsB[i];i++ ) {
if ( ai !== bi ) {
var tmpNode = ai.nextSibling; if ( tmpNode ) {
while ( tmpNode ) {
if( tmpNode === bi ){
DOMUtils.POSITION_PRECEDING return 4
} tmpNode = tmpNode.nextSibling;
}
}
DOMUtils.POSITION_FOLLOWING
return 2;
}
}
DOMUtils.POSITION_DISCONNECTED return 1;
},
Change History (6)
comment:1 Changed 14 years ago by
Component: | General → Performance |
---|
comment:2 Changed 14 years ago by
comment:3 Changed 14 years ago by
new code
getPosition : function ( nodeA, nodeB ) {
如果两个节点是同一个节点
if ( nodeA === nodeB ) {
DOMUtils.POSITION_IDENTICAL return 0;
}
var node,
parentsA = [nodeA], parentsB = [nodeB];
node = nodeA; while ( node = node.parentNode ) {
如果nodeB是nodeA的祖先节点 if ( node === nodeB ) {
DOMUtils.POSITION_IS_CONTAINED + DOMUtils.POSITION_FOLLOWING return 10;
} parentsA.push( node );
}
node = nodeB; while ( node = node.parentNode ) {
如果nodeA是nodeB的祖先节点 if ( node === nodeA ) {
DOMUtils.POSITION_CONTAINS + DOMUtils.POSITION_PRECEDING return 20;
} parentsB.push( node );
}
parentsA = parentsA.reverse(); parentsB = parentsB.reverse();
if( parentsA[0] !== parentsB[0] )
DOMUtils.POSITION_DISCONNECTED return 1;
var i = -1; while ( i++, parentsA[i] === parentsB[i] );
nodeA = parentsA[i]; nodeB = parentsB[i];
while ( nodeA = nodeA.nextSibling ) {
if( nodeA === nodeB ){
DOMUtils.POSITION_PRECEDING return 4
}
}
DOMUtils.POSITION_FOLLOWING
return 2;
},
comment:4 Changed 14 years ago by
Keywords: | HasPatch? added |
---|
comment:5 Changed 14 years ago by
Version: | 3.5.3 (SVN - trunk) → 3.0 |
---|
comment:6 Changed 13 years ago by
Resolution: | → invalid |
---|---|
Status: | new → closed |
Performance issue caused by Array::push has been patched on #7260, so node::getPosition is not subjected to change, thanks for the contribution.
Replying to campaign: