<!-- 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 treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT
);
const list = [];
while (treeWalker.nextNode()) {
list.push(treeWalker.currentNode);
}
const treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ALL,
{
acceptNode(node) {
if (node.nodeType === Node.ELEMENT_NODE | Node.TEXT_NODE) {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_REJECT;
}
}
);
const list = [];
while (treeWalker.nextNode()) {
list.push(treeWalker.currentNode);
}
const list = document.body.querySelectorAll('*');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
treeWalker with param | |
treeWalker with filter function | |
querySelectorAll |
Test name | Executions per second |
---|---|
treeWalker with param | 265328.6 Ops/sec |
treeWalker with filter function | 9579.5 Ops/sec |
querySelectorAll | 842552.2 Ops/sec |
Measuring JavaScript performance is crucial for optimizing web applications. Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares three approaches to traverse an HTML document:
treeWalker
with a parameter (NodeFilter.SHOW_ELEMENT)treeWalker
with a filter functionquerySelectorAll
Options Comparison
Here's a brief overview of each option, their pros and cons:
querySelectorAll
method to select all elements with a specified selector (in this case, '*').Library and Purpose
The NodeFilter
interface is part of the W3C DOM specification, providing a way to filter nodes during traversal. The treeWalker
method returns a tree walker object that can be used to traverse the document. In this benchmark, both options rely on the NodeFilter
interface to filter node types.
Special JS Feature or Syntax
There isn't any special JavaScript feature or syntax being tested in this benchmark. However, it's worth noting that the use of treeWalker
and querySelectorAll
relies on the W3C DOM specification, which is widely supported across modern browsers.
Other Alternatives
Some alternative approaches to traversing an HTML document include:
.each()
method or the Array.prototype.forEach()
methodKeep in mind that these alternatives may have performance characteristics different from those of tree walker and query selector approaches.
Benchmark Preparation Code
The benchmark preparation code generates two identical HTML articles with a h1 heading and p element. This allows for accurate testing of each approach's performance, as the same document is traversed using each method.
Individual Test Cases
Each test case includes a brief description and benchmark definition, which outlines the specific implementation details for that particular approach (e.g., treeWalker
with param or querySelectorAll).