开发者

serious performance problem with jquery find in firefox

Here's a simple pice of jQuery code:

alert($(document).find("*").length);
alert($(document).find("v\\:group").length);
alert($(document).find("v\\:group").find("*").length);

In non-firefox browsers, all 3 steps return instantaneously; in firefox 3.6 the third step seems to take forever (I let it run for over a minute at 100% CPU and it didn't finish). I'm using the latest jQuery (1.5.1) but had the same results in 1.4.2.

The counts returned are 10000, 50, and 2000 so you can see that whilst the document is not trivial in size, it shouldn't be big enough 开发者_JAVA技巧to present a significant performance problem like that.

In response to the previous posts, I'd just like to clarify a bit more: the document is HTML containing some embedded VML (which is IE specific). I know that jquery doesn't support namespaces properly but I had gathered that this is the best way to search for nodes outside the HTML namespace in jQuery.

What I'm stumped on is that even though I understand performance of find("") is not great, I don't see how it can take such a huge amount of time to find 2000 nodes when document.find() finds 10000 nodes instantaneously


Looks like the Firefox routine to travers the dom and return the nodes is not very performant. It semms like it can't be jQueries fault, cause other Browsers seem to handle it better.


OK I got to the bottom of this. Turns out jQuery is innocent; it's because firefox parses the HTML differently. It seems that if you have some "HTML" source like this:

<v:group />
<v:group />
<v:group />

Firefox builds the DOM like this:

<v:group>
    <v:group>
        <v:group />
    </v:group>
</v:group>

so $(document).find("v\\:group").find("*") turns from O(n) to O(n^2), hence the big difference in performance in my document with thousands of <v:group>s in.

I would have hoped firefox would have parsed it the same as other browsers but i'll stop short of calling it a bug because I don't think this is valid HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜