<!-- 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 walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
);
for (let node = walker.nextNode(), i = 0;
node;
i++, node= walker.nextNode()
) {
if(node.nodeName == 'H1') node.className = 'classy' ;
}
const elements = Array.from(document.body.querySelectorAll('h1'))
for(let i = 0, len = elements.length; i < len; i++){elements[i].className = 'classy'}
const walker = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
);
for (let node = walker.nextNode(), i = 0;
node;
i++, node= walker.nextNode()
) {
node.className = 'classy' ;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TreeWalker | |
querySelectorAll | |
NodeIterator |
Test name | Executions per second |
---|---|
TreeWalker | 559347.1 Ops/sec |
querySelectorAll | 809522.0 Ops/sec |
NodeIterator | 410099.9 Ops/sec |
The provided benchmark definition and test cases are designed to measure the performance of three different methods for filtering elements in an HTML document: TreeWalker, querySelectorAll, and NodeIterator.
TreeWalker
TreeWalker is an API that allows developers to traverse an DOM tree in a way that's similar to walking through a file system. In this benchmark, we create a TreeWalker instance on the document body with the NodeFilter.SHOW_ELEMENT
filter, which only considers elements in the traversal. We then iterate over the nodes returned by the walker and add a class name "classy" to all
querySelectorAll
querySelectorAll is an API that allows developers to select multiple elements based on their CSS selectors. In this benchmark, we use the Array.from()
method to create an array of all
NodeIterator
NodeIterator is similar to TreeWalker but allows for more flexibility in how nodes are traversed. In this benchmark, we create a NodeIterator instance on the document body with the NodeFilter.SHOW_ELEMENT
filter and then iterate over the nodes returned by the iterator, adding a class name "classy" to each element.
Options Compared
The three methods are compared based on their performance in terms of:
Pros and Cons
Here are some pros and cons for each method:
Library and Purpose
In this benchmark, the following libraries/libraries-like APIs are used:
querySelectorAll()
to select all Special JS Features
The benchmark uses the following special JavaScript features:
const
declaration and template literals....
): Used in the Array.from()
method to create an array.Other Alternatives
Some other methods that could be used for filtering elements include:
Overall, the choice of method depends on the specific use case and requirements. TreeWalker is a good option for traversing complex DOM trees, while querySelectorAll is suitable for simple selection tasks. NodeIterator offers more flexibility, but its performance can vary depending on the specific use case.