Run details:
Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Chrome 90
Windows 8.1
Desktop
3 years ago
Test name Executions per second
Traverse function 57863.2 Ops/sec
NodeIterator with filter function 13287.5 Ops/sec
NodeIterator with filter param 376473.9 Ops/sec
TreeWalker with filter function 37228.2 Ops/sec
TreeWalker with filter param 442488.9 Ops/sec
Script Preparation code:
x
 
// language=HTML
const html = `
    <!-- article out start -->
    <article>
        <!-- article in start -->
        <h1>Lorem ipsum</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque, nostrum.</p>
        <!-- article in end -->
    </article>
    <!-- article out end -->
    <!-- article out start -->
    <article>
        <!-- article in start -->
        <h1>Lorem ipsum</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque, nostrum.</p>
        <!-- article in end -->
    </article>
    <!-- article out end -->
    <!-- article out start -->
    <article>
        <!-- article in start -->
        <h1>Lorem ipsum</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque, nostrum.</p>
        <!-- article in end -->
    </article>
    <!-- article out end -->
`;
const template = document.createElement('template');
template.innerHTML = html;
window.testTemplate = template;
window.testTraverse = function traverse(node, use) {
    if (!node) return;
    use(node);
    traverse(node.firstChild, use);
    traverse(node.nextSibling, use);
}
Tests:
  • Traverse function

     
    const comments = [];
    testTraverse(testTemplate.content.firstChild, node => {
        if (node.nodeType === Node.COMMENT_NODE) {
            comments.push(node);
        }
    });
  • NodeIterator with filter function

     
    const nodeIterator = document.createNodeIterator(
        testTemplate.content,
        NodeFilter.SHOW_ALL,
        {
            acceptNode(node) {
                if (node.nodeType === Node.COMMENT_NODE) {
                    return NodeFilter.FILTER_ACCEPT;
                }
                return NodeFilter.FILTER_REJECT;
            }
        }
    );
    const comments = [];
    while (nodeIterator.nextNode()) {
        comments.push(nodeIterator.currentNode);
    }
  • NodeIterator with filter param

     
    const nodeIterator = document.createNodeIterator(
        testTemplate.content,
        NodeFilter.SHOW_COMMENT
    );
    const comments = [];
    while (nodeIterator.nextNode()) {
        comments.push(nodeIterator.currentNode);
    }
  • TreeWalker with filter function

     
    const treeWalker = document.createTreeWalker(
        testTemplate.content,
        NodeFilter.SHOW_ALL,
        {
            acceptNode(node) {
                if (node.nodeType === Node.COMMENT_NODE) {
                    return NodeFilter.FILTER_ACCEPT;
                }
                return NodeFilter.FILTER_REJECT;
            }
        }
    );
    const comments = [];
    while (treeWalker.nextNode()) {
        comments.push(treeWalker.currentNode);
    }
  • TreeWalker with filter param

     
    const treeWalker = document.createTreeWalker(
        testTemplate.content,
        NodeFilter.SHOW_COMMENT
    );
    const comments = [];
    while (treeWalker.nextNode()) {
        comments.push(treeWalker.currentNode);
    }