Rewrite getDescendants() non-recursively
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Mon, 01 Aug 2011 13:05:45 -0600
changeset 478 1fc389b77696
parent 477 304204457dab
child 479 1580a1b1f19d
Rewrite getDescendants() non-recursively

Chrome keeps freezing on insertOrderedList and insertUnorderedList
tests. When I tried pausing in the debugger, it actually worked for
once, and displayed a stack like a dozen or more deep of
getDescendants(), then it crashed the tab. Making it non-recursive
seems to improve things. (Specifically, it was calling
getDescendants(document), which is a worst case and I should probably
get rid of it.)
implementation.js
--- a/implementation.js	Mon Aug 01 12:57:26 2011 -0600
+++ b/implementation.js	Mon Aug 01 13:05:45 2011 -0600
@@ -92,9 +92,10 @@
 
 function getDescendants(node) {
 	var descendants = [];
-	for (var i = 0; i < node.childNodes.length; i++) {
-		descendants.push(node.childNodes[i]);
-		descendants = descendants.concat(getDescendants(node.childNodes[i]));
+	var stop = nextNodeDescendants(node);
+	while ((node = nextNode(node))
+	&& node != stop) {
+		descendants.push(node);
 	}
 	return descendants;
 }