<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 data-pew="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("[data-pew='child']");
$("[data-pew='child']", $element);
$("[data-pew='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" | 722063.9 Ops/sec |
find with selector and scope | 623786.6 Ops/sec |
find with selector | 245960.7 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition JSON
The provided JSON represents a benchmark test for comparing the performance of three different approaches to find an element in a DOM:
find
methodselector
(CSS selector) with scopescoped selector
( CSS selector with attribute)The HTML structure is a nested hierarchy of <div>
elements, including one containing another <div>
with a span
element that has an attribute data-pew="child"
.
Benchmark Preparation Code
The script preparation code creates a jQuery object $element
by selecting all elements with the class parent
.
Individual Test Cases
There are three test cases:
find
method to search for an element within the scope of $element
.[data-pew='child']
) with scope (the jQuery object
$element)` to search for the element.[data-pew='child']
) without specifying scope.Performance Comparison
The benchmark results show that:
find
method is the fastest, with an average of 722,063 executions per second.selector
with scope (test case 2) is slower than the find
method but faster than the scoped selector
.scoped selector
is the slowest, with an average of 245,960 executions per second.Pros and Cons
Here's a brief summary of each approach:
scoped selector
, but slower than find
. This approach allows for more flexibility in selecting elements.Best Practices
Based on these results, if you need to find an element within a specific scope, using a CSS selector with scope (test case 2) might be a good compromise between speed and flexibility. If you only need to find an element without considering scope, the find
method is likely the best choice.
Keep in mind that these results are specific to this benchmark test and may not generalize to all use cases. Always consider your specific requirements and DOM structure when choosing an approach for finding elements in JavaScript.