Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fork TreeWalker vs querySelectorAll (* all elements)2
(version: 0)
Comparing performance of:
querySelectorAll vs Tree
Created:
3 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 -->
Script Preparation code:
window.treeWalker = document.createTreeWalker( document.body, NodeFilter.SHOW_ELEMENT );
Tests:
querySelectorAll
const list = document.body.querySelectorAll('*');
Tree
const list=[]; let el; while(el=treeWalker.nextNode()){ list.push(el); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
querySelectorAll
Tree
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark compares two approaches to traverse an HTML document: using `querySelectorAll` (a method from the DOM API) versus using a custom tree walker implementation (`Fork TreeWalker`). **Options Compared** 1. **querySelectorAll**: This method returns all elements that match a given selector within a given root node. In this case, it's used to select all elements (`*`) in the `document.body`. 2. **Custom Tree Walker (Fork)**: A custom tree walker implementation is created using the `createTreeWalker` method and the `NodeFilter.SHOW_ELEMENT` filter. This implementation traverses the document body and pushes each element into a list. **Pros and Cons of Each Approach** 1. **querySelectorAll**: * Pros: + Efficient for large documents, as it uses a single DOM query. + Fast execution, thanks to browser optimizations. * Cons: + May be slower for very small documents or simple queries. + Can lead to unnecessary DOM recalculations if the selector is complex. 2. **Custom Tree Walker (Fork)**: * Pros: + More control over the traversal process, allowing for fine-grained optimization. + Can be more efficient for very large documents or specific use cases. * Cons: + More complex to implement and maintain. + May have overhead due to creating a custom tree walker. **Library Usage** In this benchmark, there is no explicit library usage. However, the `createTreeWalker` method and `NodeFilter.SHOW_ELEMENT` filter are part of the DOM API, which is a built-in JavaScript library. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax being used in this benchmark. It's a straightforward implementation of tree walking using the DOM API. **Other Considerations** * The benchmark uses an iPhone 15 device and Mobile Safari 15 as the test platform, which may introduce some biases due to differences between mobile browsers. * The use of `window.treeWalker` suggests that the custom tree walker implementation is being run in a browser environment, but it's not clear if this code will work in other environments. **Alternatives** Other alternatives for traversing HTML documents include: 1. **DocumentFragment**: A lightweight alternative to DOM elements, which can be used to traverse and manipulate document content. 2. **ElementIterator**: An iterator-like API that allows for efficient traversal of DOM elements without the need for tree walking or custom implementations. 3. **Pseudo-Element Traversal**: Some browsers support pseudo-element traversal using methods like `querySelector` with a pseudo-element selector (e.g., `:first-child`). Keep in mind that each alternative has its pros and cons, and the choice of traversal method depends on the specific use case and performance requirements.
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 (* all elements)2
Comments
Confirm delete:
Do you really want to delete benchmark?