Re: [Element Traversal LC] access to element by index

On Apr 2, 2008, at 16:21, Henri Sivonen wrote:
> It was a problem for me when I tried to walk the HTML5 spec in  
> Firefox 1.5 using iterative traversal with firstChild/nextSibling/ 
> parentNode. Firefox 1.5 performed a *lot* worse than the then- 
> current versions of Opera and Safari. And the spec was a lot smaller  
> back then, too. :-)


It turns out that I still have the test case around:
http://hsivonen.iki.fi/test/dom-walk-perf.html

I think it is perfectly reasonable to expect this algorithm to be the  
fast way to walk when you see a tree with first child, sibling and  
parent references:

function walk(node) {
   var current = node;
   var next = null;
   for (;;) {
     if (next = current.firstChild) {
        current = next;
        continue;
     }
     break;
     for (;;) {
       if (next = current.nextSibling) {
         current = next;
         break;
       }
       current = current.parentNode;
       if (current == node) {
         return;
       }
     }
   }
}

With Element Traversal, I think it would be reasonable to expect the  
above algorithm to be the fast way to walk the elements when s/ 
firstChild/firstElementChild/ and s/nextSibling/nextElementSibling/.

-- 
Henri Sivonen
hsivonen@iki.fi
http://hsivonen.iki.fi/

Received on Wednesday, 2 April 2008 13:51:26 UTC