Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TreeWalker/ filter vs querySelectorAll vs NodeIterator
(version: 0)
Comparing performance of:
TreeWalker vs querySelectorAll vs NodeIterator
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!-- 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 -->
Tests:
TreeWalker
const walker = document.createTreeWalker( document.body, NodeFilter.SHOW_ELEMENT, function(node) { return node.tagName == "H1" ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; }, false ); for (let node = walker.nextNode(), i = 0; node; i++, node= walker.nextNode() ) { node.className = 'classy' ; }
querySelectorAll
const elements = Array.from(document.body.querySelectorAll('h1')) for(let el of elements){el.className = 'classy'}
NodeIterator
const walker = document.createNodeIterator( document.body, NodeFilter.SHOW_ELEMENT, function(node) { return node.tagName == "H1" ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; }, false ); for (let node = walker.nextNode(), i = 0; node; i++, node= walker.nextNode() ) { node.className = 'classy' ; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
TreeWalker
querySelectorAll
NodeIterator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
TreeWalker
66331.3 Ops/sec
querySelectorAll
197856.9 Ops/sec
NodeIterator
56191.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark compares three different approaches to select and iterate over HTML elements: 1. **TreeWalker**: uses the `createTreeWalker` method to traverse the DOM tree, filtering elements based on their tag name. 2. **querySelectorAll**: uses the `querySelectorAll` method to select all elements with a specific tag name and then iterates over them. 3. **NodeIterator**: uses the `createNodeIterator` method to create an iterator over the DOM tree, filtering elements based on their tag name. **Options Compared** The benchmark compares three options: * **TreeWalker** + Pros: efficient use of resources, can be more accurate for complex filter conditions + Cons: may have slower performance due to overhead of creating a tree walker * **querySelectorAll** + Pros: simple and widely supported, fast performance + Cons: may not be as efficient for large datasets or complex filters * **NodeIterator** + Pros: similar to TreeWalker but with less overhead, more flexible filtering options + Cons: may have slower performance due to creation of a node iterator **Library/Feature Explanation** * **TreeWalker**: the `createTreeWalker` method creates an object that allows traversing the DOM tree in a depth-first manner. The `NodeFilter.SHOW_ELEMENT` constant filters the traversal to only include elements, and the callback function is called for each element. * **querySelectorAll**: the `querySelectorAll` method returns a NodeList containing all elements that match the specified selector. The `[el.className = 'classy']` part iterates over this list of elements. * **NodeIterator**: similar to TreeWalker, but creates an iterator object instead of a tree walker. **Special JS Feature/Syntax** No special JavaScript features or syntax are used in these benchmarks, apart from using template literals and ES6-style string concatenation (`\r\n`). **Alternatives** Other alternatives for iterating over HTML elements could include: * Using `document.body.querySelectorAll` with a CSS selector (e.g., `.h1`) * Using a library like jQuery or Lodash to simplify DOM manipulation * Using a more specialized approach, such as using `Element.prototype.querySelectorAll` or `CSSRuleList` However, these alternatives are not directly comparable to the three options tested in this benchmark. **Benchmark Insights** From the latest benchmark results, we can see that: * **querySelectorAll** is the fastest option for executing 197856.90625 times per second. * **TreeWalker** is slower than `querySelectorAll` but faster than `NodeIterator`, with 66331.3359375 executions per second. * **NodeIterator** is the slowest option, with 56191.17578125 executions per second. These results suggest that `querySelectorAll` is a good choice for simple cases where speed is prioritized over code complexity or flexibility. However, if accuracy and flexibility are more important, TreeWalker might be a better option.
Related benchmarks:
treeWalker param vs treefalker with filter function vs querySelectorAll
treeWalker param vs treefalker with filter function vs querySelectorAll edit
TreeWalker vs querySelectorAll vs NodeIterator + filter HTMLElement
Traverse function vs NodeIterator vs TreeWalker vs TreeWalker manual filter
TreeWalker vs querySelectorAll vs NodeIterator filters
Comments
Confirm delete:
Do you really want to delete benchmark?