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 |
// 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);
}
const comments = [];
testTraverse(testTemplate.content.firstChild, node => {
if (node.nodeType === Node.COMMENT_NODE) {
comments.push(node);
}
});
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);
}
const nodeIterator = document.createNodeIterator(
testTemplate.content,
NodeFilter.SHOW_COMMENT
);
const comments = [];
while (nodeIterator.nextNode()) {
comments.push(nodeIterator.currentNode);
}
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);
}
const treeWalker = document.createTreeWalker(
testTemplate.content,
NodeFilter.SHOW_COMMENT
);
const comments = [];
while (treeWalker.nextNode()) {
comments.push(treeWalker.currentNode);
}