Ok. Here's what I've found so far. First I thought the delay problem was because of the recursion. But my iterative version takes even more because of the array pushes/pops. Take a look:
Code:
function treeTraverseIterative(elem) {
var queue = new Array();
var i, currentElem, childs;
queue.push(elem);
while (queue.length > 0) {
currentElem = queue.pop();
visit(currentElem);
childs = currentElem.childNodes.length;
for (i = 0; i < childs; i++) {
queue.push(currentElem.childNodes[i]);
}
}
}
function treeTraverseRecursion(currentElem) {
var i = 0;
var currentElemChild = currentElem.childNodes[i];
if (currentElem) {
while (currentElemChild) {
treeTraverseRecursion(currentElemChild);
i++;
currentElemChild = currentElem.childNodes[i];
}
visit(currentElem);
}
}
function visit(elem) {
// visits the element
}
Now I'm reaching to this conclusion. I can stand with this tree traverse delay
, but updates to this tree takes too long. If I try to change only the text fields (nodeType == 3), on some examples, it turned out 3 times more consuming! Please.. can anyone help me do this faster?
Thanks for your attention
Bookmarks