Opened 8 years ago

Closed 8 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 8 years ago by campaign

Component: GeneralPerformance

comment:2 in reply to:  description Changed 8 years ago by campaign

Replying to campaign:

not use getAddress getIndex methods performance poor

my code

getPosition : function ( nodeA, nodeB ) {

如果两个节点是同一个节点

if ( nodeA === nodeB ) {

DOMUtils.POSITION_IDENTICAL return 0;

}

var node,

parentsA = [], parentsB = [];

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(); parentsA.push(nodeA); parentsB = parentsB.reverse(); parentsB.push(nodeB);

if( parentsA[0] !== parentsB[0] )

DOMUtils.POSITION_DISCONNECTED return 1;

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;

}

}

},

comment:3 Changed 8 years ago by campaign

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 8 years ago by Krzysztof Studnik

Keywords: HasPatch? added

comment:5 Changed 8 years ago by Krzysztof Studnik

Version: 3.5.3 (SVN - trunk)3.0

comment:6 Changed 8 years ago by Garry Yao

Resolution: invalid
Status: newclosed

Performance issue caused by Array::push has been patched on #7260, so node::getPosition is not subjected to change, thanks for the contribution.

Note: See TracTickets for help on using tickets.
© 2003 – 2019 CKSource – Frederico Knabben. All rights reserved. | Terms of use | Privacy policy