<ul id="theList">
<li class="odd">Item 1</li>
<li class="even">Item 2</li>
<li class="odd">Item 3</li>
<li class="even">Item 4</li>
<li class="odd">Item 5</li>
<li class="even">Item 6</li>
<li class="odd">Item 7</li>
<li class="even">Item 8</li>
<li class="odd">Item 9</li>
<li class="even">Item 10</li>
<li class="odd">Item 11</li>
<li class="even">Item 12</li>
<li class="odd">Item 13</li>
<li class="even">Item 14</li>
<li class="odd">Item 15</li>
<li class="even">Item 16</li>
<li class="odd">Item 17</li>
<li class="even">Item 18</li>
<li class="odd">Item 19</li>
<li class="even">Item 20</li>
<li class="odd">Item 21</li>
<li class="even">Item 22</li>
<li class="odd">Item 23</li>
<li class="even">Item 24</li>
<li class="odd">Item 25</li>
<li class="even">Item 26</li>
<li class="odd">Item 27</li>
<li class="even">Item 28</li>
<li class="odd">Item 29</li>
<li class="even">Item 30</li>
</ul>
var els = $('.even');
var els = document.querySelectorAll('.even');
var els = document.getElementsByClassName('even');
var els = $('li.even');
var els = document.querySelectorAll('li.even');
var els = document.getElementById("theList").querySelectorAll('.even');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery | |
querySelectorAll | |
getElementsByClassName | |
jQuery: mixed selector | |
querySelectorAll: mixed selector | |
with Id |
Test name | Executions per second |
---|---|
jQuery | 320069.2 Ops/sec |
querySelectorAll | 61764.3 Ops/sec |
getElementsByClassName | 2749203.5 Ops/sec |
jQuery: mixed selector | 47755.9 Ops/sec |
querySelectorAll: mixed selector | 57745.8 Ops/sec |
with Id | 545648.7 Ops/sec |
Let's dive into the benchmark and explore what is being tested.
Benchmark Definition
The benchmark compares three methods of obtaining a group of elements identified by class name:
The purpose of this benchmark is to compare the performance of these three methods in different scenarios, such as when an ID is present or not.
Options Compared
Here's a breakdown of what each option does:
$('[selector]')
) to select elements. When an ID is present, it uses the #
symbol followed by the ID.querySelectorAll()
method to select elements. It can accept a CSS selector as a string argument and returns a NodeList of matching elements.getElementsByClassName()
method to select elements. It takes a class name or space-separated list of class names as an argument.Pros and Cons
Here's a brief overview of each option:
Library Used
The jQuery library is used in the first two test cases (jQuery
and jQuery: mixed selector
). The querySelectorAll native JavaScript method is used in the next three test cases (querySelectorAll
, querySelectorAll: mixed selector
, and with Id
). The getElementsByClassName native JavaScript method is used in the last two test cases (getElementsByClassName
and jQuery: mixed selector
).
Performance Comparison
The benchmark shows the performance of each option across different browsers and devices. The results indicate that:
Keep in mind that these results may vary depending on the specific use case, browser versions, and other factors.