<div class="test1">
<div class="test1">
<div class="test1"></div>
<div class="test1"></div>
<div class="test1">
<div class="test1">
<div class="test1"></div>
</div>
</div>
<div class="test1"></div>
<div class="test1"></div>
</div>
<div class="test1">
<div class="test1"></div>
<div class="test1">
<div class="test1">
<div class="test1"></div>
</div>
</div>
<div class="test1">
<div class="test1">
<div class="test1"></div>
</div>
</div>
<div class="test1"></div>
</div>
<div class="test1"></div>
<div class="test1">
<div class="test1"></div>
<div class="test1">
<div class="test1">
<div class="test1"></div>
</div>
<div class="test1"></div>
</div>
<div class="test1"></div>
<div class="test1"></div>
<div class="test1"></div>
</div>
<div class="test1"></div>
<div class="test1">
<div class="test1">
<div class="test1"></div>
<div class="test1">
<div class="test1">
<div class="test1">
<article class="test" id="test"></article>
</div>
<div class="test1">
<div class="test1"></div>
</div>
<div class="test1"></div>
</div>
<div class="test1"></div>
</div>
</div>
<div class="test1"></div>
<div class="test1"></div>
</div>
<div class="test1">
<div class="test1"></div>
<div class="test1">
<div class="test1"></div>
<div class="test1"></div>
</div>
<div class="test1"></div>
<div class="test1"></div>
<div class="test1"></div>
</div>
</div>
document.getElementById("test");
document.querySelector("#test")
document.getElementsByClassName("test")
document.querySelector(".test")
document.getElementsByTagName("test")
document.querySelector("article")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getElementById | |
querySelector (id) | |
getElementsByClassName | |
querySelector (class) | |
getElementsByTagName | |
querySelector (tag) |
Test name | Executions per second |
---|---|
getElementById | 4264977.0 Ops/sec |
querySelector (id) | 1265958.5 Ops/sec |
getElementsByClassName | 4380357.0 Ops/sec |
querySelector (class) | 1210883.8 Ops/sec |
getElementsByTagName | 4360594.0 Ops/sec |
querySelector (tag) | 1278573.2 Ops/sec |
Let's break down what's being tested in the provided JSON and explain the pros and cons of each approach.
What's being tested:
The benchmark is comparing the performance of four DOM selection methods:
getElementById
querySelector
with an ID selector (#test
)getElementsByClassName
with a class selector (class="test"
)getElementsByTagName
with a tag selector (tag="test"
)Additionally, one more test case is being compared: querySelector
with a tag selector (article
).
Options being compared:
getElementById
: This method selects the first element with the specified ID.querySelector
:#test
): Selects the first element with the specified ID.class="test"
): Returns an array of all elements that match the specified class.getElementsByClassName
:getElementsByTagName
:Special JS feature:
None mentioned in this benchmark, but it's worth noting that querySelector
has become a standard method for DOM selection in modern JavaScript (since ECMAScript 2009).
Benchmark results:
The latest benchmark results show the performance of each method on an Android device with Firefox Mobile 122. The top performers are:
getElementsByClassName
with a class selector (class="test"
): ~4380000 executions/secondgetElementsByTagName
with a tag selector (tag="test"
): ~4360000 executions/secondquerySelector
with an ID selector (#test>
): ~1278000 executions/secondgetElementById
: ~1210000 executions/secondPlease note that these results may vary depending on the specific device, browser version, and system configuration.
In general, the benchmark suggests that:
getElementsByClassName
with a class selector is likely to be the fastest method for this test case.getElementsByTagName
with a tag selector is close in performance but slightly slower.querySelector
with an ID selector and getElementById
are slower due to caching issues.