<div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div>
<div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div>
<a class="parent" >
<div><div><div></div><div><div><div></div><div><div><div>
<span class="child">Need span</span>
<div></div></div></div></div></div></div></div><div></div></div>
<svg class="icon md-16 icon-shevron-right text-muted"></svg>
</a>
<div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div><div><div><div></div><div><div><div></div><div><div><div>
<span>pew</span>
<div></div></div></div></div></div></div></div><div></div></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js'></script>
var $element = $(".parent");
$element.find(".child");
$(".child", $element);
$(".child");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
find with "find" | |
find with selector and scope | |
find with selector |
Test name | Executions per second |
---|---|
find with "find" | 746905.8 Ops/sec |
find with selector and scope | 597162.9 Ops/sec |
find with selector | 611574.8 Ops/sec |
Let's dive into the explanation of this JavaScript microbenchmark.
Benchmark Purpose
The benchmark tests three different ways to select an element using jQuery:
find()
method$(".child")
)$()
function (e.g., $(".child", $element)
)Options Compared
The benchmark compares the performance of each option, measuring how many executions per second they can handle.
Pros and Cons
find()
method: This method traverses the DOM tree from the current element to find the target element. It's useful when you need to search for an element within a larger container.querySelectorAll()
function to select elements that match the specified CSS selector. It's fast and efficient, but can be slower if the selection is too complex.$()
function: This method uses the $()
function to create a new jQuery object, which allows you to search within a specific scope (e.g., $(".child", $element)
). It's useful when you need to select elements within a larger container.find()
method if searching within a large DOM tree.Library and Purpose
The benchmark uses jQuery, which is a popular JavaScript library for DOM manipulation and event handling. The $()
function is the primary entry point for interacting with the DOM using jQuery.
Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax that are specific to modern browsers (e.g., async/await, Promises, Web Workers). However, it's worth noting that some of these features may affect performance in certain scenarios.
In summary, this benchmark provides a simple yet informative comparison of three jQuery methods for selecting elements: find()
, CSS selector, and scoped selector with $()
function.