<div class="test" id="test"></div>
document.querySelector(".test")
document.querySelectorAll(".test")
document.getElementsByClassName(".test")
document.querySelector("#test")
document.getElementById("test")
document.getElementsByTagName('div');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
querySelector | |
querySelectorAll | |
getElementsByClassName | |
querySelector (ID) | |
getElementsByID | |
getElementsByTagName |
Test name | Executions per second |
---|---|
querySelector | 3834753.5 Ops/sec |
querySelectorAll | 1805169.2 Ops/sec |
getElementsByClassName | 4817523.5 Ops/sec |
querySelector (ID) | 2266917.8 Ops/sec |
getElementsByID | 4280555.0 Ops/sec |
getElementsByTagName | 5069459.0 Ops/sec |
Overview of the Benchmark
MeasureThat.net provides a JavaScript microbenchmarking platform where users can compare the performance of different DOM query methods in JavaScript. The benchmark tests six query methods:
document.querySelector()
document.querySelectorAll()
document.getElementsByClassName()
document.querySelector()
with an ID selector (#test
)document.getElementById()
(Note: This is not a standard DOM method, it's likely a typo and should be document.querySelector()
with an ID selector)document.getElementsByTagName('div')
Options Compared
The benchmark compares the performance of each query method:
document.querySelector()
: Selects the first element matching the specified selector.document.querySelectorAll()
: Returns a NodeList containing all elements matching the specified selector.document.getElementsByClassName()
: Returns a DOMCollection (a legacy API) containing all elements with the specified class name.document.querySelector()
with ID selector: Selects the element with the specified ID.document.getElementById()
: Not used in the benchmark, but could be an alternative to document.querySelector()
with an ID selector.document.getElementsByTagName('div')
: Returns a DOMCollection containing all elements of type 'DIV'.Pros and Cons
Here's a brief summary of each query method:
document.querySelector()
: Fast and efficient, but only returns the first matching element. Pros: lightweight, fast. Cons: may not find all matching elements if there are multiple.document.querySelectorAll()
: Returns all matching elements, but may be slower than querySelector()
. Pros: finds all matching elements. Cons: can be slower due to the need to process all matches.document.getElementsByClassName()
: Legacy API that's still supported for backward compatibility. Pros: widely supported, easy to use. Cons: deprecated, not as efficient as other methods.document.querySelector()
with ID selector: Fast and efficient when selecting an element by ID. Pros: lightweight, fast. Cons: only works if the element exists in the DOM.document.getElementById()
: Not used in the benchmark, but could be an alternative to document.querySelector()
with an ID selector. Would likely have similar performance characteristics.Library and Special Features
None of these query methods rely on specific libraries or features beyond the standard DOM API. However, it's worth noting that some browsers may have additional features or optimizations for certain query methods (e.g., Chrome's "querySelector" optimization).
Other Alternatives
In modern JavaScript development, you might consider using more efficient query methods like:
document.querySelector()
with a CSS selector syntax (e.g., .test
)document.querySelectorAll()
with a CSS selector syntaxElement.contains()
or Array.prototype.filter()
For most use cases, the standard DOM query methods are sufficient and efficient enough. However, if you're working on complex web applications with high-performance requirements, you may want to explore more advanced techniques or consider using a JavaScript framework or library that provides optimized query methods.