Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TreeWalker for loop -> vs querySelectorAll (* ) ->
(version: 0)
Comparing performance of:
TreeWalker vs querySelectorAll
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 treeWalker = document.createTreeWalker( document.body, NodeFilter.SHOW_ELEMENT ); const list = []; for(let element; element; element = treeWalker.nextNode()) { element.className = 'new Class' }
querySelectorAll
const list = document.body.querySelectorAll('*'); for(let i = 0, len = list.length ; i < len ; i++){ list[i].className = 'new Class' }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
TreeWalker
querySelectorAll
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of two approaches to modify elements in HTML documents: using a `TreeWalker` and using `querySelectorAll`. The benchmark aims to determine which approach is faster. **TreeWalker Approach** The TreeWalker approach uses the `createTreeWalker()` method to traverse the DOM tree. A `NodeFilter.SHOW_ELEMENT` filter is used to only consider element nodes. In each iteration, the next node is retrieved using the `nextNode()` method and its class name is modified to "new Class". **querySelectorAll Approach** The `querySelectorAll('*')` approach uses a CSS selector to select all elements in the document. The selected elements are then iterated over, and their class names are modified to "new Class" using a `for` loop. **Comparison of Approaches** Here's a comparison of the two approaches: * **TreeWalker Approach** * Pros: * More explicit control over the traversal order * Can be more efficient for modifying elements in a specific order * Cons: * May require more setup and configuration * Can be slower for very large documents due to the overhead of creating a TreeWalker instance * **querySelectorAll Approach** * Pros: * Generally faster than the TreeWalker approach, especially for large documents * More concise and easier to read code * Cons: * Less control over the traversal order * May modify elements in a different order than intended **Library Usage** The `TreeWalker` approach uses the `document.createTreeWalker()` method, which is part of the DOM API. This method creates a new instance of a TreeWalker object, which traverses the DOM tree according to the specified filter. **Special JS Features** None are mentioned in the provided code or benchmark results. **Alternatives** Other alternatives for modifying elements in HTML documents include: * **getElementsByClassName()**: A DOM method that returns an array of elements with a specific class name. It can be used as an alternative to `querySelectorAll`, but it may have performance issues for very large documents. * **CSS Selectors**: CSS selectors can be used to select elements based on various attributes, classes, or IDs. However, they may not provide the same level of control as the TreeWalker approach. **Other Considerations** When choosing an approach, consider factors such as: * Document size: For very large documents, the `querySelectorAll` approach may be faster due to its optimized implementation. * Element modification order: If a specific order is required, the TreeWalker approach may provide more control. * Code readability and conciseness: The `querySelectorAll` approach can result in more concise code. Overall, the choice between the TreeWalker and `querySelectorAll` approaches depends on the specific requirements of your project.
Related benchmarks:
treeWalker param vs treefalker with filter function vs querySelectorAll edit
Copy of TreeWalker vs querySelectorAll (* all elements)
Traverse function vs NodeIterator vs TreeWalker vs TreeWalker manual filter
TreeWalker for loop vs querySelectorAll (* )
TreeWalker vs querySelectorAll(getting first and list child) v2
Comments
Confirm delete:
Do you really want to delete benchmark?