Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Traverse function vs NodeIterator vs TreeWalker for TEXT nodes
(version: 0)
Let's compare the speed of 3 different ways to traverse the DOM.
Comparing performance of:
Traverse function vs NodeIterator vs TreeWalker
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// language=HTML const html = ` <!-- 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 template = document.createElement('template'); template.innerHTML = html; window.testTemplate = template; window.testTraverse = function traverse(node, use) { if (!node) return; use(node); traverse(node.firstChild, use); traverse(node.nextSibling, use); }
Tests:
Traverse function
const textNodes = []; testTraverse(testTemplate.content.firstChild, node => { if (node.nodeType === Node.SHOW_TEXT) { textNodes.push(node); } });
NodeIterator
const nodeIterator = document.createNodeIterator( testTemplate.content, NodeFilter.SHOW_TEXT ); const textNodes = []; while (nodeIterator.nextNode()) { textNodes.push(nodeIterator.currentNode); }
TreeWalker
const treeWalker = document.createTreeWalker( testTemplate.content, NodeFilter.SHOW_TEXT ); const textNodes = []; while (treeWalker.nextNode()) { textNodes.push(treeWalker.currentNode); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Traverse function
NodeIterator
TreeWalker
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 dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different methods to traverse text nodes in an HTML document: 1. **Traverse Function**: A custom function that recursively visits each node in the DOM, allowing for a user-defined callback. 2. **NodeIterator**: An API provided by web browsers to iterate over nodes in the DOM, specifically designed for traversing text content. 3. **TreeWalker**: Another API provided by web browsers to traverse the DOM, similar to NodeIterator but with more features and flexibility. **Options Compared** The benchmark compares these three approaches: * **Pros of each approach:** + Traverse Function: Customizable, can be optimized for specific use cases. + NodeIterator: Simple, efficient, and widely supported. + TreeWalker: More powerful, flexible, and suitable for complex traversals. * **Cons of each approach:** + Traverse Function: May require additional memory allocation, has overhead due to recursive function calls. + NodeIterator: Limited control over traversal behavior, may not handle complex nodes correctly. + TreeWalker: Steeper learning curve, more complex implementation. **Library Usage** None of the benchmark tests explicitly use any libraries. However, it's worth noting that `Node` and its properties (e.g., `SHOW_TEXT`) are part of the JavaScript API provided by web browsers. **Special JS Features** No special JavaScript features or syntax are used in this benchmark. The focus is on comparing different traversal methods. **Alternative Approaches** Other alternatives for traversing text nodes include: * **DOMParser**: Can be used to parse HTML and extract text content. * **XMLSerializer**: Similar to DOMParser, but designed for XML documents. * **Regular expressions**: Can be used to search for patterns in text content, but may not provide the same level of control as traversal methods. These alternatives might be more suitable depending on specific use cases or requirements. For example, using a regular expression might be faster than traversing the DOM, but it may not be as flexible or accurate. Overall, this benchmark provides a useful comparison between three common methods for traversing text nodes in an HTML document, allowing developers to choose the best approach for their specific needs.
Related benchmarks:
Traverse function vs NodeIterator vs TreeWalker
Traverse function vs NodeIterator vs TreeWalker1
Traverse function vs NodeIterator vs TreeWalker vs TreeWalker manual filter
Traverse function vs NodeIterator vs TreeWalker (elements)
Comments
Confirm delete:
Do you really want to delete benchmark?