<!-- 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 = [];
let element;
while (element = treeWalker.nextNode()) {
list.push(element);
}
const elements = list.filter(el => el instanceof HTMLElement);
const elements = Array.from(document.body.querySelectorAll('*')).filter(node => node instanceof HTMLElement);
const nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
);
const list = [];
let element;
while (element = nodeIterator.nextNode()) {
list.push(element);
}
const elements = list.filter(el => el instanceof HTMLElement);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TreeWalker | |
querySelectorAll | |
NodeIterator |
Test name | Executions per second |
---|---|
TreeWalker | 957828.6 Ops/sec |
querySelectorAll | 1055229.0 Ops/sec |
NodeIterator | 966471.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided benchmark compares three methods to retrieve all HTML elements from an article element in a web page:
Options compared
The benchmark compares the performance of these three methods, which can be summarized as follows:
Pros and Cons
Here's a summary of the pros and cons of each method:
Library and syntax
The benchmark uses the following libraries and syntax:
document.createTreeWalker()
: Creates a new TreeWalker object that traverses the DOM in a tree-like structure.Array.from()
and querySelectorAll('*')
: These methods are part of the modern JavaScript API, introduced in ECMAScript 2015 (ES6).NodeIterator
and filter()
: These methods are also part of the modern JavaScript API, introduced in ECMAScript 2014 (ES6).Other considerations
When choosing between these methods, consider the following factors:
Alternatives
If you're looking for alternatives to these methods, consider:
querySelector()
or getElementById()
: These methods are simpler and more straightforward but may not be as efficient as the other options.DOMParser
API: This API allows parsing HTML documents from a string, which can be useful in certain scenarios.In summary, this benchmark compares three methods for retrieving all HTML elements from an article element in a web page. TreeWalker is suitable for complex documents, while querySelectorAll and NodeIterator + filter HTMLElement are better suited for large documents or simple documents with performance requirements.