<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<a></a>
<a></a>
<a></a>
<a></a>
<a></a>
<a></a>
<a></a>
<a></a>
<a></a>
<a></a>
document.querySelectorAll('a').forEach((element) => {});
document.querySelectorAll('a').forEach((element) => {});
const tags = document.getElementsByTagName('a');
const l = tags.length;
for (let i = 0; i < l; i++) {
const element = tags[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
querySelectorAll | |
getElementsByTagName |
Test name | Executions per second |
---|---|
querySelectorAll | 286286.0 Ops/sec |
getElementsByTagName | 883055.8 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Overview
The benchmark measures the performance difference between two DOM query methods: querySelectorAll()
and getElementsByTagName()
. Both methods are used to select elements from an HTML document. The test is run on a constant number of elements, which means that the number of elements returned by each method is fixed.
Test Cases
There are two individual test cases:
querySelectorAll
: This test case uses the document.querySelectorAll('a').forEach((element) => {})
script preparation code to select all anchor elements (<a>
) from the HTML document and iterate over them using a forEach
loop.getElementsByTagName
: This test case uses the following script preparation code: const tags = document.getElementsByTagName('a'); const l = tags.length; for (let i = 0; i < l; i++) { const element = tags[i]; }
Options Compared
The benchmark compares the performance of two query methods:
querySelectorAll()
: This method returns a NodeList containing all elements that match the specified CSS selector ('a'
). The NodeList is live, meaning it updates in real-time when the DOM changes.getElementsByTagName()
: This method returns an HTMLCollection (an array-like object) containing all elements of the specified tag name ('a'
). The HTMLCollection is not live and does not update when the DOM changes.Pros and Cons
Here are some pros and cons of each approach:
querySelectorAll()
:getElementsByTagName()
in older browsers or those with strict security policies.getElementsByTagName()
:Library/Functionality
There are no external libraries used in this benchmark. The forEach
method is a built-in JavaScript function that iterates over an iterable object (in this case, the NodeList returned by querySelectorAll()
).
Special JS Features/Syntax
The benchmark uses the following special JavaScript features:
forEach
: A built-in JavaScript method for iterating over arrays or iterable objects.const
: A keyword used to declare constants in modern JavaScript.Other Alternatives
If you need to measure the performance of other DOM query methods, here are some alternatives:
getElementById()
: Returns a single element by its ID attribute.getElementByTagName()
(alternative name for getElementsByTagName()
): Returns an HTMLCollection of elements with the specified tag name.querySelector()
and querySelectorAll()
(without the document.
prefix): Can be used to select elements using CSS selectors in modern browsers.Keep in mind that each method has its own strengths and weaknesses, and the choice of which one to use depends on your specific use case and requirements.