<div id="my-id">
<div class="grand">
<div class="parent"> Awesome stuff! </div>
<div class="my-class">
<p> I like carrots! </p>
</div>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
$(' div#my-id div.grand div.parent div.my-class ').css("background-color", "#cccccc"); //case-1
$(' #my-id .grand .parent div.my-class ').css("background-color", "#cccccc"); //case-2
$(' #my-id .grand .parent .my-class ').css("background-color", "#cccccc"); //case-3
$(' div#my-id div.my-class ').css("background-color", "#cccccc"); //case-4
$(' #my-id div.my-class ').css("background-color", "#cccccc"); //case-5
$(' #my-id .my-class ').css("background-color", "#cccccc"); //case-6
$(' div.my-class ').css("background-color", "#cccccc"); //case-7
$(' .my-class ').css("background-color", "#cccccc"); //case-8
$(' div#my-id div.grand div.parent div.my-class ').css("background-color", "#cccccc");
$(' #my-id .grand .parent div.my-class ').css("background-color", "#cccccc");
$(' #my-id .grand .parent .my-class ').css("background-color", "#cccccc");
$(' div#my-id div.my-class ').css("background-color", "#cccccc");
$(' #my-id div.my-class ').css("background-color", "#cccccc");
$(' #my-id .my-class ').css("background-color", "#cccccc");
$(' div.my-class ').css("background-color", "#cccccc");
$(' .my-class ').css("background-color", "#cccccc");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
case-1 | |
case-2 | |
case-3 | |
case-4 | |
case-5 | |
case-6 | |
case-7 | |
case-8 |
Test name | Executions per second |
---|---|
case-1 | 2066756.4 Ops/sec |
case-2 | 2162077.0 Ops/sec |
case-3 | 2204930.0 Ops/sec |
case-4 | 971905.1 Ops/sec |
case-5 | 968203.8 Ops/sec |
case-6 | 990843.1 Ops/sec |
case-7 | 1010453.2 Ops/sec |
case-8 | 1026676.1 Ops/sec |
Measuring the performance of JavaScript selectors can be a complex task, as it depends on various factors such as the browser, device platform, operating system, and even the specific hardware configuration.
Benchmark Definition
The provided benchmark measures the performance of different selector notation styles in jQuery 2. The script preparation code creates an HTML structure with multiple elements, each selected using a different notation style (e.g., div#my-id div.grand div.parent div.my-class
, #my-id .grand .parent div.my-class
, etc.). The benchmark then measures the execution time of setting the background color of these elements.
Options compared
The benchmark compares the performance of six different selector notation styles:
div#my-id div.grand div.parent div.my-class
#my-id .grand .parent div.my-class
.grand .parent div.my-class
div#my-id div.my-class
#my-id > div.grand .parent div.my-class
(not used in this benchmark)[attr="class"]
(not used in this benchmark)Pros and Cons
Browser-specific optimizations
The benchmark results suggest that Chrome 107 is consistently faster than other browsers (e.g., Firefox, Edge) across all selector notation styles. This might be due to Chrome's optimized JavaScript engine or specific browser-specific features like div#my-id
being cached more efficiently.
Takeaways
Please note that these findings are specific to this particular benchmark and may not generalize to other use cases or scenarios.