1

以下代码在以下浏览器中运行缓慢:

  • 铬 26.0.1410.65 (Mac)
  • Safari 6.0.4 (Mac)

HTML:

<ul></ul>

<script type="text/html" id="template">
<li>
  Lorem ipsum dolor…
</li>
</script>

咖啡脚本:

# Setup
list = $ 'ul'
templateText = $('#template').text()

# Append 1000 list items
list.append templateText for i in [1..1000]

…但是如果我从模板中去掉空格和换行符,它运行得非常快。

自己试试:http: //jsfiddle.net/cEK2x/2/

Firefox 26.0 (Mac)、Chrome Canary 28.0.1500.3 (Mac) 和 IE9 不会出现此问题。

4

1 回答 1

0

Ryosuke Niwa from the WebKit mailing list pointed me to the bug filed in the WebKit tracker:

The reason is that we do not create renderers for empty text nodes and thus we are hitting the worst O(N2) case in Node::attach(). (See FIXME in Node::attach().)

This patch adds a logic to bail out the loop to avoid the O(N2) case. Specifically, the patch bails out the loop if we encounter a text node for which we again decided not to create a renderer. This bail out is reasonable because the fact that we again decided not to create a renderer for the text node indicates that there will be no affect of the result of Text::textRendererIsNeeded() of the rest of the sibling nodes.

于 2013-05-08T17:29:39.437 回答