<!-- 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 treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT
);
const list = [];
while (treeWalker.nextNode()) {
list.push(treeWalker.currentNode);
}
const treeWalker = document.createTreeWalker(
testTemplate.content,
NodeFilter.SHOW_ALL,
{
acceptNode(node) {
if (node.nodeType === Node.ELEMENT_NODE | Node.TEXT_NODE) {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_REJECT;
}
}
);
const list = [];
while (treeWalker.nextNode()) {
list.push(treeWalker.currentNode);
}
const list = document.body.querySelectorAll('*');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
treeWalker with param | |
treeWalker with filter function | |
querySelectorAll |
Test name | Executions per second |
---|---|
treeWalker with param | 403518.0 Ops/sec |
treeWalker with filter function | 0.0 Ops/sec |
querySelectorAll | 964592.0 Ops/sec |
Let's break down the benchmark and its options.
Overview
The benchmark tests three different ways to traverse and retrieve elements from an HTML document: using document.createTreeWalker
with a parameter, using treeWalker
with a filter function, and using querySelectorAll
.
Options Compared
document.createTreeWalker
with a parameter: This method takes the root node of the document (in this case, the body
element) and an optional parameter specifying which type of nodes to consider (NodeFilter.SHOW_ELEMENT
). It returns a tree walker object that allows iterating over the nodes in the document.treeWalker
with filter function: This method creates a tree walker object just like the first option, but instead of passing a node filter, it passes an anonymous function (acceptNode
) that defines how to filter nodes based on their type and other criteria.querySelectorAll
: This is a static method of the Document
interface that returns all elements matching a CSS selector.Pros and Cons
document.createTreeWalker
with a parameter:treeWalker
with filter function:querySelectorAll
:Library and Special JS Feature
In this benchmark, NodeFilter
is a built-in JavaScript API that allows filtering nodes based on their type and other criteria. It's used in the first two options to control which node types are considered by the tree walker.
No special JavaScript features or syntax are used in this benchmark beyond what's described above.
Alternatives
Other alternatives for traversing and retrieving elements from an HTML document include:
querySelector
: Similar to querySelectorAll
, but only returns the first matching element.getElementsByClassName
or getElementsByTagName
: These methods return a live HTMLCollection of elements with the specified class name or tag name, respectively.Array.from()
and Document.querySelectorAll()
: This method creates an array-like object from a live HTMLCollection returned by querySelectorAll
.In general, the choice of approach depends on your specific requirements, performance needs, and the complexity of your document structure.