<div class="he" name="hed">
<div class="ho" name="as">
<div class="unique" name="unique" >test</div>
<div class="unique" name="unique" >test</div>
</div>
<div class="unique" name="unique" >test</div>
<div class="unique" name="unique" >test</div>
</div>
var doc = document;
var test = doc.querySelectorAll('.unique');
var test = doc.querySelectorAll('[name="unique"]');
var test = doc.getElementsByName('unique');
var test = doc.getElementsByClassName('unique');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
querySelectorAll/class | |
querySelectorAll/name | |
getElementsByName | |
getElementsByClassName |
Test name | Executions per second |
---|---|
querySelectorAll/class | 521600.8 Ops/sec |
querySelectorAll/name | 281547.8 Ops/sec |
getElementsByName | 2672466.8 Ops/sec |
getElementsByClassName | 2623820.2 Ops/sec |
What is being tested?
On the provided JSON, three DOM query methods are being compared: querySelectorAll
, getElementsByName
, and getElementsByClassName
. These methods are used to select elements from an HTML document.
Options being compared:
querySelectorAll
: Returns a NodeList containing all child elements that match the specified CSS selector. It can also accept a string with attributes, like [name="unique"]
.getElementsByName
: Returns a collection of elements whose name attribute matches the specified value.getElementsByClassName
: Returns a live HTMLCollection of elements whose class attribute contains the specified value.Pros and Cons:
querySelectorAll
.querySelectorAll
, only matching by name.querySelectorAll
, only matching by class.Library usage:
None mentioned in this benchmark, but note that these methods are built-in DOM APIs and do not rely on external libraries.
Special JS features or syntax:
There is no explicit mention of any special JavaScript features or syntax being used in this benchmark. However, it's worth noting that the use of CSS selectors (querySelectorAll
) requires modern browsers (e.g., Chrome 90), as older browsers may not support them.
Other alternatives:
If you're looking for alternative methods to achieve similar results:
querySelectorAll
in older browsers.Keep in mind that the choice of method depends on your specific use case, performance requirements, and personal preference.