<div class="test" id="test-1"></div>
<div class="test" id="test-2"></div>
<div class="test" id="test-3"></div>
<div class="test" id="test-4"></div>
<div class="test" id="test-5"></div>
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
document.querySelector(".test")
document.getElementsByClassName(".test")
document.getElementById("#test-1")
document.querySelector("[id^='test-']");
document.querySelectorAll("[id^='test-']");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
querySelector class | |
getElementsByClassName | |
getElementById | |
querySelector with wildcards | |
querySelectorAll with wildcards |
Test name | Executions per second |
---|---|
querySelector class | 3746736.8 Ops/sec |
getElementsByClassName | 5951979.5 Ops/sec |
getElementById | 6026779.0 Ops/sec |
querySelector with wildcards | 3368481.0 Ops/sec |
querySelectorAll with wildcards | 1247716.4 Ops/sec |
The benchmark JSON provided outlines a performance comparison of various DOM selection methods in JavaScript when selecting elements based on their class names and IDs. Let's dive into the specifics of the tests conducted, the options compared, their pros and cons, and alternative methods for DOM selection.
document.getElementById("#test-1")
document.getElementsByClassName(".test")
document.querySelector(".test")
getElementById
.getElementById
.document.querySelector("[id^='test-']")
document.querySelectorAll("[id^='test-']")
querySelector
but retrieves all matching elements at once.The benchmark results indicated the following approximate execution speeds, which indicate how many times per second each method can be executed on average:
getElementById
(45,159,868 executions/sec)getElementsByClassName
(21,228,334 executions/sec)querySelector
(17,285,412 executions/sec)querySelector
with wildcards (10,772,074 executions/sec)querySelectorAll
with wildcards (3,558,710.5 executions/sec)While the methods tested are common in JavaScript for selecting DOM elements, other alternatives exist. For instance:
Using libraries like jQuery: This library provides methods like $(".test")
which encapsulate the complexity of DOM selection and offer a more consistent and cross-browser compatible API. However, it adds the overhead of another library.
Using modern frameworks: Libraries and frameworks like React, Vue, or Angular manage DOM interactions differently, often abstracting direct DOM access away from the developer. This can help maintain performance but comes with a learning curve.
The benchmark effectively highlights performance differences between standard DOM querying techniques in vanilla JavaScript. While getElementById
proves to be the most efficient, querySelector
and querySelectorAll
provide flexibility at the cost of speed. Each method has its best use cases, and understanding them can help software engineers choose the optimal one depending on their application's requirements.