@samitouri / QOS-React-2 / commits / 87db648f3e

Address pull request feedback.

Ben Newman committed Sep 24, 2013 at 16:07 UTC 87db648f3e1c0a7edfc5809ff0803f981264d7ae
1 file changed +17 -6
src/core/ReactMount.js
+17 -6
@@ -46,8 +46,8 @@ if (__DEV__) {
46 var rootElementsByReactRootID = {};
47 }
48
49 -/** Used to store breadth-first search state in findComponentRoot. */
50 -var reusableArray = [];
49 +// Used to store breadth-first search state in findComponentRoot.
50 +var findComponentRootReusableArray = [];
51
52 /**
53 * @param {DOMElement} container DOM element that may contain a React component.
@@ -541,11 +541,11 @@ var ReactMount = {
541 * @internal
542 */
543 findComponentRoot: function(ancestorNode, id) {
544 - var firstChildren = reusableArray;
544 + var firstChildren = findComponentRootReusableArray;
545 var childIndex = 0;
546
547 - firstChildren.length = 0;
548 - firstChildren.push(ancestorNode.firstChild);
547 + firstChildren[0] = ancestorNode.firstChild;
548 + firstChildren.length = 1;
549
550 while (childIndex < firstChildren.length) {
551 var child = firstChildren[childIndex++];
@@ -553,17 +553,27 @@ var ReactMount = {
553 var childID = ReactMount.getID(child);
554 if (childID) {
555 if (id === childID) {
556 + // Emptying firstChildren/findComponentRootReusableArray is
557 + // not necessary for correctness, but it helps the GC reclaim
558 + // any nodes that were left at the end of the search.
559 firstChildren.length = 0;
560 +
561 return child;
558 - } else if (ReactInstanceHandles.isAncestorIDOf(childID, id)) {
562 + }
563 +
564 + if (ReactInstanceHandles.isAncestorIDOf(childID, id)) {
565 // If we find a child whose ID is an ancestor of the given ID,
566 // then we can be sure that we only want to search the subtree
567 // rooted at this child, so we can throw out the rest of the
568 // search state.
569 firstChildren.length = childIndex = 0;
570 firstChildren.push(child.firstChild);
571 +
572 + // Ignore the rest of this child's siblings and immediately
573 + // continue the outer loop with child.firstChild as child.
574 break;
575 }
576 +
577 } else {
578 // If this child had no ID, then there's a chance that it was
579 // injected automatically by the browser, as when a `<table>`
@@ -572,6 +582,7 @@ var ReactMount = {
582 // branch, but not before examining the other siblings.
583 firstChildren.push(child.firstChild);
584 }
585 +
586 child = child.nextSibling;
587 }
588 }