Skip to content

Commit

Permalink
Remove ReactID.primeTree in favor of priming in ReactID.getID.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeffmo authored and zpao committed Jun 25, 2013
1 parent c1886c6 commit 1c40dde
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions src/core/ReactID.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ var nodeCache = {};
function getID(node) {
if (node && node.getAttributeNode) {
var attributeNode = node.getAttributeNode(ATTR_NAME);
return attributeNode && attributeNode.value || '';
if (attributeNode) {
var id = attributeNode.value;
if (id) {
// Assume that any node previously cached with this ID has since
// become invalid and should be replaced. TODO Enforce this.
nodeCache[id] = node;

return id;
}
}
}

return '';
Expand Down Expand Up @@ -79,31 +88,6 @@ function getNode(id) {
return null;
}

/**
* Efficiently finds all nodes with a React-specific ID and primes the
* cache so that getNode lookups take constant time.
*
* @param {DOMElement} root The root element to scan.
*/
function primeTree(root) {
var nodes = root.querySelectorAll
? root.querySelectorAll('[' + ATTR_NAME + ']')
: root.getElementsByTagName('*');

for (var i = 0; i < nodes.length; ++i) {
prime(nodes.item(i));
}

prime(root);
}

function prime(node) {
var attributeNode = node.getAttributeNode(ATTR_NAME);
if (attributeNode) {
nodeCache[attributeNode.value] = node;
}
}

/**
* Causes the cache to forget about one React-specific ID.
*
Expand All @@ -124,6 +108,5 @@ exports.ATTR_NAME = ATTR_NAME;
exports.getID = getID;
exports.setID = setID;
exports.getNode = getNode;
exports.primeTree = primeTree;
exports.purgeID = purgeID;
exports.purgeEntireCache = purgeEntireCache;

0 comments on commit 1c40dde

Please sign in to comment.