@samitouri / QOS-React-2 / commits / 1c40dde782
Fork 0

The library for web and native user interfaces.

Remove ReactID.primeTree in favor of priming in ReactID.getID.

Although it would have been nice to prime the entire tree and achieve a cache hit rate of 100%, that cost would have to be paid up front, during page rendering. This patch avoids priming up front in favor of making the most of the work done by `ReactMount.findReactRenderedDOMNodeSlow`, which calls `ReactID.getID` while traversing the rendered DOM. The insight is this: if `getID` simply primes the cache whenever it finds a new ID, then `findReactRenderedDOMNodeSlow` will end up priming quite a few more nodes that are actually involved in `ReactID.getNode` lookups, and we won't need `primeTree` at all.

CommitSyncScript committed Jun 25, 2013 at 13:56 UTC 1c40dde7825e7d8863d69357dbd7013f926e35f6
1 file changed +10 -27
src/core/ReactID.js
+10 -27
@@ -36,7 +36,16 @@ var nodeCache = {};
36 function getID(node) {
37 if (node && node.getAttributeNode) {
38 var attributeNode = node.getAttributeNode(ATTR_NAME);
39 - return attributeNode && attributeNode.value || '';
39 + if (attributeNode) {
40 + var id = attributeNode.value;
41 + if (id) {
42 + // Assume that any node previously cached with this ID has since
43 + // become invalid and should be replaced. TODO Enforce this.
44 + nodeCache[id] = node;
45 +
46 + return id;
47 + }
48 + }
49 }
50
51 return '';
@@ -79,31 +88,6 @@ function getNode(id) {
88 return null;
89 }
90
82 -/**
83 - * Efficiently finds all nodes with a React-specific ID and primes the
84 - * cache so that getNode lookups take constant time.
85 - *
86 - * @param {DOMElement} root The root element to scan.
87 - */
88 -function primeTree(root) {
89 - var nodes = root.querySelectorAll
90 - ? root.querySelectorAll('[' + ATTR_NAME + ']')
91 - : root.getElementsByTagName('*');
92 -
93 - for (var i = 0; i < nodes.length; ++i) {
94 - prime(nodes.item(i));
95 - }
96 -
97 - prime(root);
98 -}
99 -
100 -function prime(node) {
101 - var attributeNode = node.getAttributeNode(ATTR_NAME);
102 - if (attributeNode) {
103 - nodeCache[attributeNode.value] = node;
104 - }
105 -}
106 -
91 /**
92 * Causes the cache to forget about one React-specific ID.
93 *
@@ -124,6 +108,5 @@ exports.ATTR_NAME = ATTR_NAME;
108 exports.getID = getID;
109 exports.setID = setID;
110 exports.getNode = getNode;
127 -exports.primeTree = primeTree;
111 exports.purgeID = purgeID;
112 exports.purgeEntireCache = purgeEntireCache;