@samitouri / QOS-React-2 / commits / 6b9fc81b64

Space optimizations for ReactMount.findComponentRoot.

Ben Newman committed Sep 23, 2013 at 11:19 UTC 6b9fc81b642df27d43327e1cc606867ba5cddab4
1 file changed +10 -6
src/core/ReactMount.js
+10 -6
@@ -46,6 +46,9 @@ if (__DEV__) {
46 var rootElementsByReactRootID = {};
47 }
48
49 +/** Used to store breadth-first search state in findComponentRoot. */
50 +var reusableArray = [];
51 +
52 /**
53 * @param {DOMElement} container DOM element that may contain a React component.
54 * @return {?string} A "reactRoot" ID, if a React component is rendered.
@@ -538,15 +541,19 @@ var ReactMount = {
541 * @internal
542 */
543 findComponentRoot: function(ancestorNode, id) {
541 - var firstChildren = [ancestorNode.firstChild];
544 + var firstChildren = reusableArray;
545 var childIndex = 0;
546
547 + firstChildren.length = 0;
548 + firstChildren.push(ancestorNode.firstChild);
549 +
550 while (childIndex < firstChildren.length) {
551 var child = firstChildren[childIndex++];
552 while (child) {
553 var childID = ReactMount.getID(child);
554 if (childID) {
555 if (id === childID) {
556 + firstChildren.length = 0;
557 return child;
558 } else if (ReactInstanceHandles.isAncestorIDOf(childID, id)) {
559 // If we find a child whose ID is an ancestor of the given ID,
@@ -556,11 +563,6 @@ var ReactMount = {
563 firstChildren.length = childIndex = 0;
564 firstChildren.push(child.firstChild);
565 break;
559 - } else {
560 - // TODO This should not be necessary if the ID hierarchy is
561 - // correct, but is occasionally necessary if the DOM has been
562 - // modified in unexpected ways.
563 - firstChildren.push(child.firstChild);
566 }
567 } else {
568 // If this child had no ID, then there's a chance that it was
@@ -574,6 +576,8 @@ var ReactMount = {
576 }
577 }
578
579 + firstChildren.length = 0;
580 +
581 if (__DEV__) {
582 console.error(
583 'Error while invoking `findComponentRoot` with the following ' +