<div id="testdiv06">
<div id="unique06" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv05">
<div id="unique05" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv04">
<div id="unique04" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv03">
<div id="unique03" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv02">
<div id="unique02" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv01">
<div id="unique01" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv">
<div id="unique" class="unique" name="unique" data-unique="1">test</div>
</div>
var i, imax;
var doc = document;
var test = doc.getElementById('testdiv').childNodes[0].innerHTML;
var formelem = doc.getElementById('testdiv').getElementsByTagName('div')[0].innerHTML;
var test = doc.getElementById('testdiv').querySelector('.unique').innerHTML;
var test = doc.getElementById('testdiv').getElementsByClassName('unique')[0].innerHTML;
var test = doc.getElementsByName('unique')[0].innerHTML;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getElementById | |
getElementsByTagName | |
querySelector | |
getElementsByClassName | |
getElementsByName |
Test name | Executions per second |
---|---|
getElementById | 2715493.2 Ops/sec |
getElementsByTagName | 1150559.8 Ops/sec |
querySelector | 1426911.9 Ops/sec |
getElementsByClassName | 1159674.9 Ops/sec |
getElementsByName | 1321494.1 Ops/sec |
The provided JSON represents a benchmark test for comparing the performance of different methods to retrieve a single DOM element in JavaScript.
Methods being compared:
getElementById
querySelector
getElementsByClassName
getElementsByTagName
(which is not actually used, as it's not getting a single element but an array of elements)Options and their pros/cons:
getElementById
:querySelector
:getElementById
, allows for more complex selectors.getElementsByClassName
:getElementsByTagName
(not actually used):Libraries and their purposes:
Special JS features or syntax:
None mentioned in the provided JSON, as none of the methods being compared require special JavaScript features or syntax beyond standard DOM manipulation.
Other alternatives:
If you're looking for alternative methods to retrieve a single DOM element, you might consider:
document.querySelector
(same as querySelector
, but with a different API)elementFromPoint
(a method that returns the nearest ancestor element from a given point on the page)Keep in mind that these alternatives may have different trade-offs in terms of performance, flexibility, and browser support.