<script src="//code.jquery.com/jquery-3.4.0.slim.min.js"></script>
<div id="#basket-content">
<ul class="nav nav-tabs">
<li>
<a href="#content-prints" data-toggle="tab" class="font-16">
<i class="fa fa-file-photo-o m-r-10"></i>
</a>
</li>
</ul>
</div>
var el = $("#basket-content ul.nav li");
var className = el.className;
var el = $("#basket-content .nav li");
var className = el.className;
var el = $("#basket-content ul li");
var className = el.className;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery(#id) | |
jQuery(.id) | |
jQuery(tag.id) |
Test name | Executions per second |
---|---|
jQuery(#id) | 439952.4 Ops/sec |
jQuery(.id) | 417377.9 Ops/sec |
jQuery(tag.id) | 371407.0 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Overview
The benchmark compares the speed of getting an element by ID using jQuery (a popular JavaScript library) versus Vanilla JS ( plain JavaScript without any libraries). The benchmark is focused on three specific test cases:
#id
)>
or ~
selectors ((.id)
and (tag.id)
, respectively)(tag.id)
)Test Cases
Each test case is represented as a JSON object, which includes:
Benchmark Definition
: The actual JavaScript code used to measure the performance of getting an element by ID.Test Name
: A brief description of the test case.Let's analyze each test case individually:
var el = $("#basket-content ul.nav li");
var className = el.className;
This test case uses jQuery to select the direct descendant element with the ID "basket-content ul.nav li". The #
symbol is used to specify that the selector should match an element by its ID.
Pros:
Cons:
var el = $( ".nav li" );
var className = el.className;
This test case uses jQuery to select an ancestor element with the class "nav li" followed by the ID "basket-content". The .
symbol is used to specify that the selector should match an element by its class.
Pros:
jQuery(#id)
, but also allows for more flexibility in selecting ancestor elements.Cons:
var el = $( "#basket-content ul li" );
var className = el.className;
This test case uses jQuery to select an ancestor element with the tag name "ul li" followed by the ID "basket-content". The (tag.id)
syntax is used to specify that the selector should match an element by its tag name and then its ID.
Pros:
Cons:
Other Considerations
Alternatives
If you're looking for alternatives to jQuery, consider using other lightweight JavaScript libraries or plain vanilla JavaScript solutions. Some popular alternatives include:
dom-element
and get-el
Keep in mind that these alternatives might have different performance characteristics, so it's essential to benchmark and test them thoroughly.