Skip to content

Commit

Permalink
Space optimizations for ReactMount.findComponentRoot.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Dec 3, 2013
1 parent e4909d0 commit 6b9fc81
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/core/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ if (__DEV__) {
var rootElementsByReactRootID = {};
}

/** Used to store breadth-first search state in findComponentRoot. */
var reusableArray = [];

/**
* @param {DOMElement} container DOM element that may contain a React component.
* @return {?string} A "reactRoot" ID, if a React component is rendered.
Expand Down Expand Up @@ -538,15 +541,19 @@ var ReactMount = {
* @internal
*/
findComponentRoot: function(ancestorNode, id) {
var firstChildren = [ancestorNode.firstChild];
var firstChildren = reusableArray;
var childIndex = 0;

firstChildren.length = 0;
firstChildren.push(ancestorNode.firstChild);

while (childIndex < firstChildren.length) {
var child = firstChildren[childIndex++];
while (child) {
var childID = ReactMount.getID(child);
if (childID) {
if (id === childID) {
firstChildren.length = 0;
return child;
} else if (ReactInstanceHandles.isAncestorIDOf(childID, id)) {
// If we find a child whose ID is an ancestor of the given ID,
Expand All @@ -556,11 +563,6 @@ var ReactMount = {
firstChildren.length = childIndex = 0;
firstChildren.push(child.firstChild);
break;
} else {
// TODO This should not be necessary if the ID hierarchy is
// correct, but is occasionally necessary if the DOM has been
// modified in unexpected ways.
firstChildren.push(child.firstChild);
}
} else {
// If this child had no ID, then there's a chance that it was
Expand All @@ -574,6 +576,8 @@ var ReactMount = {
}
}

firstChildren.length = 0;

if (__DEV__) {
console.error(
'Error while invoking `findComponentRoot` with the following ' +
Expand Down

0 comments on commit 6b9fc81

Please sign in to comment.