Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
NodeIterator - filter function vs nextNode state check
(version: 0)
Comparing performance of:
nextNode vs filter
Created:
4 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;
Tests:
nextNode
const nodeIterator = document.createNodeIterator( testTemplate.content, NodeFilter.SHOW_ALL ); while (nodeIterator.nextNode()) { let node = nodeIterator.referenceNode; if(node.textContent.includes('Lorem')) { node.textContent = "Lorem"; } }
filter
const nodeIterator = document.createNodeIterator( testTemplate.content, NodeFilter.SHOW_ALL, { acceptNode(node) { return node.textContent.includes('Lorem'); } } ); while (nodeIterator.nextNode()) { let node = nodeIterator.referenceNode; node.textContent = "Lorem"; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
nextNode
filter
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 JSON data and explain what is tested on it. **Benchmark Definition** The benchmark definition describes two different approaches to test the performance of NodeIterator in JavaScript: 1. `nextNode`: This approach uses a traditional while loop to iterate over the node iterator, checking each node's text content for the presence of the string "Lorem" using the `includes()` method. 2. `filter`: This approach uses the same node iterator, but wraps it with a custom filter function that checks if the current node's text content includes the string "Lorem". The filter function is used to iterate over the nodes. **Options Compared** The two approaches compared are: * Using a traditional while loop to iterate over the node iterator (`nextNode`) * Wrapping the node iterator with a custom filter function to skip nodes that don't contain the desired text (`filter`) **Pros and Cons of Each Approach** 1. `nextNode`: * Pros: Simple, straightforward implementation. * Cons: May lead to inefficient looping over the entire node iterator if many nodes are skipped. 2. `filter`: * Pros: More efficient, as only relevant nodes are processed by the filter function. * Cons: Requires creating a custom filter function, which may add overhead. **Library Used** The benchmark uses the `NodeFilter` API, specifically the `SHOW_ALL` and `acceptNode()` methods. The `NodeFilter` API is used to specify filters for node iteration, allowing developers to skip nodes that don't meet certain conditions. **Special JS Feature/Syntax** None mentioned in this specific benchmark definition. However, it's worth noting that the use of template literals (`\r\n <!-- article out start -->\r\n`) and the creation of a custom filter function demonstrate modern JavaScript features. **Other Alternatives** If you were to rewrite this benchmark using a different approach, here are some alternatives: * Using `Array.from()` to convert the node iterator to an array, which could provide faster iteration but may lead to memory issues for large datasets. * Implementing a more complex filtering logic that takes into account additional node properties or attributes. Keep in mind that these alternatives might not necessarily outperform or underperform the original benchmarked approaches.
Related benchmarks:
TreeWalker vs querySelectorAll vs NodeIterator + filter HTMLElement
TreeWalker/ filter vs querySelectorAll vs NodeIterator /filter
TreeWalker/ filter vs querySelectorAll vs NodeIterator
TreeWalker vs querySelectorAll vs NodeIterator filters
Comments
Confirm delete:
Do you really want to delete benchmark?