// 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;
var html = document.querySelector('html');
var walker = document.createTreeWalker(html, NodeFilter.SHOW_ALL);
var node;
while (node = walker.nextNode()) {
console.log(node);
}
var vv = 'count';
var data = {count:1};
var xpat = document.evaluate(`//*[contains(translate(text(),' ', ''), "{{${vv}}}") or contains(translate(@value,' ', ''), "{{${vv}}}")]`, document, null, XPathResult.ANY_TYPE, null);
while(node = xpat.iterateNext()){
console.log(node);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createTreeWalker | |
XPATH |
Test name | Executions per second |
---|---|
createTreeWalker | 1383.2 Ops/sec |
XPATH | 6976.5 Ops/sec |
Let's dive into the world of JavaScript benchmarks and explore what's being tested on MeasureThat.net.
The provided benchmark compares three ways to traverse the DOM (Document Object Model):
document.createTreeWalker()
: This method creates an iterator that traverses the DOM tree, allowing you to iterate over nodes in a specific order.//*[contains(translate(text(),' ', ''), \"{{${vv}}}\")]
selects elements that contain the value of vv
.var html = document.querySelector('html');
: This method uses CSS selectors to select an element by its tag name or ID.Options Comparison
Let's examine each option:
document.createTreeWalker()
: This approach provides direct access to the DOM tree structure, allowing for efficient traversal. However, it may not be suitable for complex document structures or large datasets.var html = document.querySelector('html');
: This approach uses CSS selectors, which are widely supported but may not be efficient for large or deeply nested documents.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
document.createTreeWalker()
var html = document.querySelector('html');
Library: XPath
The XPATH expression is evaluated using the document.evaluate()
method, which takes several parameters:
document
)ANY_TYPE
for any type of node)Special JS Feature: Spread Operator
The spread operator ({{${vv}}}
) is used in the XPATH expression to dynamically reference a variable vv
. This syntax allows values to be interpolated into the XPath expression at runtime.
Benchmark Results
According to the latest benchmark results, XPATH outperforms document.createTreeWalker()
on this specific test case. However, keep in mind that these results may vary depending on the underlying document structure and JavaScript engine optimizations.
Alternative Approaches
If you're looking for alternative methods to traverse the DOM, consider the following:
const html = new DOMParser().parseFromString(htmlString, 'text/html');
: This approach uses a DOMParser
to parse an HTML string into a document object.const doc = new DOMDocument(); doc.appendChild(doc.createElement('html'));
: This method creates a new DOM document and appends the root element to it.These alternatives might be useful in specific situations, but they may not provide the same level of performance or ease of use as the original approaches.