<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<div class="a">
<div class="b">
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
<div class="d"></div>
<div class="c">
</div>
</div>
var el = document.querySelectorAll('.d');
var el = $('.d')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Vanilla JS | |
JQuery 3.3.1 |
Test name | Executions per second |
---|---|
Vanilla JS | 454046.3 Ops/sec |
JQuery 3.3.1 | 148079.8 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark compares the performance of two approaches to select elements with a class d
:
querySelectorAll
method from the Document Object Model (DOM).$
function, which is a shortcut for jQuery's DOM manipulation API.Script Preparation Code
The script preparation code includes a reference to the jQuery library:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
This suggests that the benchmark is using an external jQuery library, rather than including it as a module or using a bundler.
Individual Test Cases
The individual test cases are:
querySelectorAll
method to select elements with the class d
. This is a standard DOM API function that returns a NodeList of matching elements.$
function to select elements with the class d
. This is a shortcut for jQuery's DOM manipulation API, which allows for more concise and expressive selection syntax.Pros and Cons
Here are some pros and cons of each approach:
querySelectorAll
): ($
):Library: jQuery
The jQuery 3.3.1
library is a popular JavaScript framework that provides a convenient and flexible way to manipulate the DOM. It includes features like event handling, animations, and Ajax requests, in addition to its DOM manipulation capabilities.
Special JS feature: No special syntax or features are being tested in this benchmark.
Other alternatives for selecting elements with a class include:
document.querySelector('.d')
: A modern alternative to querySelectorAll
, which is more concise but still widely supported.document.querySelectorAll('.d')
: The standard DOM API function, which is lightweight and widely supported.Keep in mind that the choice of selection method ultimately depends on the specific requirements of your project and personal preference.