Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TreeWalker vs querySelectorAll vs NodeIterator --2
(version: 0)
Comparing performance of:
TreeWalker vs querySelectorAll vs NodeIterator
Created:
2 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, ); for (let node = walker.nextNode(), i = 0; node; i++, node= walker.nextNode() ) { if(node.nodeName == "article") node.className = 'classy' ; }
querySelectorAll
const elements = Array.from(document.body.querySelectorAll('article')) for(let el of elements){el.className = 'classy'}
NodeIterator
const walker = document.createNodeIterator( document.body, NodeFilter.SHOW_ELEMENT, ); for (let node = walker.nextNode(), i = 0; node; i++, node= walker.nextNode() ) { if(node.nodeName == "article") 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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser/OS:
Firefox 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
TreeWalker
117434.5 Ops/sec
querySelectorAll
317936.6 Ops/sec
NodeIterator
155008.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Benchmark Definition** The benchmark is comparing three different approaches to traverse and modify elements within an HTML document: 1. `TreeWalker` 2. `querySelectorAll` (also known as the "DOM Traversing API") 3. `NodeIterator` **Options Compared** Each approach has its own pros and cons: * **TreeWalker**: This is a low-level, DOM-centric approach that allows for fine-grained control over traversal. However, it can be slower due to the overhead of creating and managing the tree walker object. + Pros: High precision, flexible, and suitable for complex traversals. + Cons: Can be slower, requires more code, and has more dependencies (e.g., `document.createTreeWalker`). * **querySelectorAll**: This is a higher-level API that uses CSS selectors to traverse the DOM. It's generally faster than TreeWalker but less flexible. + Pros: Faster, easier to use, and widely supported. + Cons: Less precise, limited control over traversal, and may not work as expected for complex traversals. * **NodeIterator**: Similar to TreeWalker, NodeIterator is a low-level API that allows for fine-grained control over traversal. However, it's less well-known and more cumbersome to use than TreeWalker. + Pros: High precision, flexible, and suitable for complex traversals. + Cons: Can be slower due to the overhead of creating and managing the node iterator object. **Library and Purpose** * `querySelectorAll` is a part of the DOM Traversing API, which provides a set of methods for navigating and querying elements within an HTML document. Its purpose is to simplify the process of traversing the DOM using CSS selectors. * `NodeIterator` is also part of the DOM Traversing API, providing a way to iterate over the nodes in the DOM while allowing developers to control the traversal. **Special JS Feature or Syntax** The benchmark uses JavaScript features such as `const`, arrow functions (`=>`), and template literals (`\r\n`) which are part of ECMAScript 2015 (ES6) syntax. These features provide improved code readability, conciseness, and maintainability. **Other Alternatives** If you need to traverse and modify elements within an HTML document, other alternatives include: * `getElementsByClassName`: A method that returns a collection of elements with the specified class name. * `document.querySelectorAll`: A method that returns a NodeList or HTMLCollection of elements matching the specified CSS selectors. * Third-party libraries like jQuery, which provide more extensive DOM traversal and manipulation capabilities. Keep in mind that each approach has its own trade-offs and may be better suited for specific use cases.
Related benchmarks:
treeWalker param vs treefalker with filter function vs querySelectorAll
treeWalker param vs treefalker with filter function vs querySelectorAll edit
Traverse function vs NodeIterator vs TreeWalker vs TreeWalker manual filter
TreeWalker vs querySelectorAll vs NodeIterator
TreeWalker vs querySelectorAll(getting first and list child) v2
Comments
Confirm delete:
Do you really want to delete benchmark?