Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this code for iterating over the DOM stupid slow?
    primarykey
    data
    text
    <p>This is nested about 10 functions deep, so I'll just paste the relevant bits:</p> <p>This line is really slow:</p> <pre><code>var nodes = Filter_Chunk(Traverse(), chunks.First()); </code></pre> <p>Specifically, this chunk inside <code>Filter_Chunk</code> (pun not intended):</p> <pre><code>private static IEnumerable&lt;HtmlNode&gt; Filter_Chunk(IEnumerable&lt;HtmlNode&gt; nodes, string selectorChunk) { // ... string tagName = selectorChunk; foreach (var node in nodes) if (node.Name == tagName) yield return node; </code></pre> <p>There's nothing too complicated in there... so I'm thinking it must be the sheer number of nodes in <code>Traverse()</code> right?</p> <pre><code>public IEnumerable&lt;HtmlNode&gt; Traverse() { foreach (var node in _context) { yield return node; foreach (var child in Children().Traverse()) yield return child; } } public SharpQuery Children() { return new SharpQuery(_context.SelectMany(n =&gt; n.ChildNodes).Where(n =&gt; n.NodeType == HtmlNodeType.Element), this); } </code></pre> <p>I tried finding <code>&lt;h3&gt;</code> nodes on <a href="https://stackoverflow.com/questions/4138803/wheres-the-bug-in-this-tree-traversal-code">stackoverflow.com</a>. There shouldn't be more than a couple thousand nodes, should there? Why is this taking many minutes to complete?</p> <hr> <p>Actually, there's definitely a bug in here somewhere that is causing it to return more nodes than it should... <a href="https://stackoverflow.com/questions/4138803/wheres-the-bug-in-this-tree-traversal-code">I forked the question to address the issue</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload