- From: Ian Sanders <notifications@github.com>
- Date: Wed, 27 Sep 2023 15:20:28 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/147/1738174441@github.com>
I agree, these APIs definitely definitely aren't perfect. There is confusing overlap between `whatToShow` and `filter`, the difference between `NodeFilter.FILTER_REJECT` and `NodeFilter.FILTER_SKIP` is definitely not intuitive, and the difference between `TreeWalker` and `NodeIterator` is surprisingly subtle. And, of course, they don't support the iterator protocol despite one of them literally being called 'Iterator' 😄. I still think they can be useful -- it's nice to have a built-in API to give you the confidence you're not making a significant performance blunder. I wouldn't mind keeping them around with some improvements. > If you compare `[javascript] NodeIterator` and `[javascript] querySelector` on Stack Overflow the difference is quite stark. I'm not convinced the comparison is valid here. If you need to iterate over a flat array of elements, `querySelectorAll` is a fine alternative. But `NodeIterator` isn't limited to `HTMLElement` -- it also can include text nodes, `Attr` nodes, CDATA, etc. The `Text` node use case in particular makes this API necessary in some scenarios. > I mean the alternative is to just write the iteration code yourself, right? This is a fair point -- a simple lazy tree-walker can be built with a recursive generator: ```js function* walkNodeTree(root) { yield root; for (const node of root.childNodes) yield* walkNodeTree(node); } ``` This isn't hard, but it's also not completely trivial. Add on support for skipping individual nodes / rejecting node subtrees and the logic starts to get a little more complex. > I think the first step here would be demonstrating why it's so bad to write loops in JavaScript. It seems to me that the primary advantage of the original feature request (adding iterator support) would actually be that it makes these APIs _easier_ to use with loops (`for (const node of iterator {...}`). So this request is an argument _for_ loops, not against them. -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/dom/issues/147#issuecomment-1738174441 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/dom/issues/147/1738174441@github.com>
Received on Wednesday, 27 September 2023 22:20:33 UTC