Skip to content

Commit

Permalink
Slightly faster findAnnotatedNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Dec 4, 2015
1 parent b03a30e commit 43fc853
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/lib/annotations/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,22 @@

// instance-time

_localSubTree: function(node, host) {
return (node === host) ? node.childNodes :
(node._lightChildren || node.childNodes);
},

findAnnotatedNode: function(root, annote) {
// recursively ascend tree until we hit root
var parent = annote.parent &&
Polymer.Annotations.findAnnotatedNode(root, annote.parent);
// unwind the stack, returning the indexed node at each level
return !parent ? root :
Polymer.Annotations._localSubTree(parent, root)[annote.index];
if (parent) {
// note: marginally faster than indexing via childNodes
// (http://jsperf.com/childnodes-lookup)
for (var n=parent.firstChild, i=0; n; n=n.nextSibling) {
if (annote.index === i++) {
return n;
}
}
} else {
return root;
}
}

};
Expand Down

0 comments on commit 43fc853

Please sign in to comment.