<!-- 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 = [];
let element;
while (element = treeWalker.nextNode()) {
list.push(element);
}
const list = document.body.querySelectorAll('*');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TreeWalker | |
querySelectorAll |
Test name | Executions per second |
---|---|
TreeWalker | 867365.9 Ops/sec |
querySelectorAll | 1939394.1 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark compares two approaches: treeWalker
(NodeIterator) and querySelectorAll
. Both methods are used to select elements from an HTML document, but they differ in their approach.
TreeWalker (nodeIterator
)
TreeWalker
is a built-in JavaScript API that allows you to traverse an XML or HTML document. It's used to iterate over the child nodes of a node, and it provides more control over the traversal process compared to querySelectorAll
.
The benchmark creates a TreeWalker
instance on the document.body
element, filtering only elements (NodeFilter.SHOW_ELEMENT
). The test then iterates over the selected elements using the nextNode()
method, pushing each element onto an array.
querySelectorAll
querySelectorAll
is a CSS selector-based API that allows you to select multiple elements from an HTML document. It returns a live HTMLCollection, which is a collection of nodes that can be traversed like an array.
In this benchmark, querySelectorAll
is used to select all elements (*
) from the document.body
. The test then assigns the result to a variable and does nothing with it (i.e., no iteration or processing).
Pros and Cons
Here are some pros and cons of each approach:
TreeWalker (nodeIterator
)
Pros:
Cons:
querySelectorAll
Pros:
Cons:
Other Considerations
The benchmark uses a simple HTML document with multiple article elements, which may not accurately represent real-world scenarios. In a real-world scenario, you might want to consider factors like document size, complexity, and caching.
Additionally, the TreeWalker
approach may require more CPU cycles due to the need for iteration over each node, whereas querySelectorAll
can take advantage of browser caching and optimization techniques.
Library/Module Used
None are explicitly mentioned in this benchmark. However, NodeIterator is a built-in JavaScript API.
Special JS Feature/Syntax
There doesn't seem to be any special JavaScript features or syntax used in this benchmark. The tests appear to use standard JavaScript APIs.
Alternatives
Some alternatives for selecting elements from an HTML document include:
querySelector
: A faster and more convenient alternative to querySelectorAll
, which selects only one element.getElementsByClassName
/getElementsByTagName
: DOM methods that select elements by class name or tag name, respectively.Array.prototype.slice()
: Can be used to create a shallow copy of an HTMLCollection.These alternatives may have different performance characteristics and use cases compared to the benchmarked approaches.