Re: Allow to return same NodeList object for queries like getElementsByTagName, getElementsByClassName and getElementsByName

On Feb 12, 2010, at 8:29 AM, Boris Zbarsky wrote:

>
> Test 1: ~4350ms
> Test 2: ~2100ms
> Test 3: ~80ms
> Test 4: ~10ms
>
> and in Opera 10.5 pre alpha:
>
> Test 1: ~520ms
> Test 2: ~3809ms
> Test 3: ~541ms
> Test 4: ~3828ms
>
> and in Safari 4:
>
> Test 1: ~260ms
> Test 2: ~1309ms
> Test 3: ~131ms (?)
> Test 4: ~20ms
>
> Given that webkit doesn't cache the nodelist, I'm not sure how to  
> explain the Safari numbers; perhaps Maciej can do that.

We cache the "guts" of the NodeList, so we save all the tree walking  
and a chunk of the object creation cost in the case where caching is  
possible. More specifically, we save some of the C++ level allocations  
but not the JS-level allocations, and GC allocation is pretty  
expensive. As you can see, that still doesn't get us as fast as  
Mozilla's full caching, even though we are faster in the uncached case.

Philip Taylor also discovered that doing getElementsByTagName in a  
loop is a reasonably common pattern, here are some real-world examples  
he cited:

http://www.hotelgoal.com/city/vn/hotel-in-bai-chay.htm
   for(i=0; i< document.getElementById(frm).getElementsByTagName 
('SELECT').length; i++){
        if(document.getElementById(frm).getElementsByTagName('SELECT') 
[i].value==0){

http://www.esiweb.org/index.php?lang=fr&id=280&portrait_ID=66&function=print
   while (document.getElementsByTagName("span")[i] != null) {
     if (document.getElementsByTagName("span")[i].className
                                                   == "fn") {

  http://google.com/codesearch?q=getElementsByTagName%5C%28%5B%5E%29%5D%2B%5C%29%5C%5B%5Ba-zA-Z%5D

Regards,
Maciej

Received on Friday, 12 February 2010 17:03:48 UTC