Калашмат Автомашникова (ak_47) wrote in lj_dev,
Калашмат Автомашникова
ak_47
lj_dev

IE7 hangs with ~100% CPU consumption

Hello,

As you probably already know, IE7 hangs with ~100% CPU consumption on almost every LJ entry (especially those with many comments) for quite a long time. I tried to look into the problem and figured out that most of the time IE7 spends within DOM.filterElementsByClassName and DOM.filterElementsByTagName functions. Both functions are located in "http://www.livejournal.com/js??dom.js" file. Particularly, offending line is
var e = es[ i ];
in both cases, where es is a collection of DOM elements obtained via document.getElementsByTagName("*") call most of the time.

I have some content filtering software installed on my machine. With its help I patched these two functions on every incoming LJ page with the following code:
DOM.filterElementsByClassName =
function( es, className ) {
    var filtered = [];
    for( var en = new Enumerator(es); !en.atEnd(); en.moveNext() ) {
        var e = en.item();
        if( DOM.hasClassName( e, className ) )
            filtered.push(e);
    }
    return filtered;
}

DOM.filterElementsByTagName =
function( es, tagName ) {
    if( tagName == "*" )
        return es;
    var filtered = [];
    tagName = tagName.toLowerCase();
    for( var en = new Enumerator(es); !en.atEnd(); en.moveNext() ) {
        var e = en.item();
        if( e.tagName && e.tagName.toLowerCase() == tagName )
            filtered.push(e);
    }
    return filtered;
}
As you can see, I just replaced collection iteration, so instead of property access operator [] the Enumerator object is used. Also, addition of new element to filtered array is replaced with filtered.push(e); instead of filtered[filtered.length] = e;.

The above changes greatly improved performance and eliminated IE7 hanging almost completely.

Now, I'm aware of the fact that Enumerator object is MS non-ECMA extension to JavaScript. I'm not sure whether similar object exists in other popular browsers (FF, Opera, Safari, etc.). However, IE6-7 is one of the most widespread browsers in the world today. I think it makes above mentioned fix worthwhile, at least for IEx clients.

Thanks.

Update: Recently I updated Windows Script on my machine to newer version: 5.7. After the update IE hangs much less severely. Here's the list of appropriate downloads:
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 39 comments