<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="maincontainer">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Long service leave</h1>
</div>
</div>
</div>
<article class="content-page">
<div class="container">
<div class="row">
<div class="col-md-8 articleContent">
<div id="content_container_540237">
<h2 id="What-is-long-service-leave">What is long service leave</h2><h2 id="Entitlement-(not-mining-employees)">Entitlement (not mining employees)</h2><h2 id="Continuous-employment">Continuous employment</h2><h2 id="Cashing-in-long-service-leave">Cashing-in long service leave</h2><h2 id="Payment-for-long-service-leave">Payment for long service leave</h2><h2 id="Casual-and-part-time-employees">Casual and part-time employees</h2>
</div>
<span class="updated">Updated: 13th September 2019</span>
</div>
<div class="col-md-4 rightHandMenu">
<div class="pagewidget">
<div class="pagewidget__controls">
<button class="pagewidget__favourite" id="item_540235">
<i class="far fa-heart"></i>
<span>Add favourite</span></button>
<i class="fas fa-heart"></i>
<span>1</span></a>
<button class="pagewidget__print" onclick="window.print()">
<i class="fas fa-print"></i>
<span>Print<span>
</span></span></button>
</div>
<div class="link-list related-links" id="onThisPage">
<h2 class="pagewidget__heading">On this page</h2>
<ul></ul>
</div>
</div>
</div>
</div>
</div>
</article>
</div>
var list = $('#onThisPage ul');
$(".articleContent h2").each(function() {
// $(this).prepend('<a id="' + $(this).text().replace(/ /g,'-') + '"></a>');
$(this).attr('id', $(this).text().replace(/ /g,'-'));
$(list).append('<li><a href="#' + $(this).text().replace(/ /g,'-') + '">' + $(this).text() + '</a></li>');
});
$("#maincontainer .articleContent h2").each(function() {
$(this).attr('id', $(this).text().replace(/ /g,'-'));
$(list).append('<li><a href="#' + $(this).text().replace(/ /g,'-') + '">' + $(this).text() + '</a></li>');
});
$("#maincontainer h2").each(function() {
$(this).attr('id', $(this).text().replace(/ /g,'-'));
$(list).append('<li><a href="#' + $(this).text().replace(/ /g,'-') + '">' + $(this).text() + '</a></li>');
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
c1 | |
c2 |
Test name | Executions per second |
---|---|
c1 | 580.0 Ops/sec |
c2 | 297.6 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and discussed.
Benchmark Definition
The benchmark is designed to compare the performance of two approaches:
#maincontainer .articleContent h2
: This approach targets elements with both an ID selector (#
) and a CSS class selector (articleContent
).#maincontainer h2
: This approach targets only elements with an ID selector (#
) but no additional CSS class selectors.Script Preparation Code
The script preparation code is used to set up the environment for the benchmark. It:
onThisPage
using jQuery..articleContent h2
and:-
) to create a unique ID.Html Preparation Code
The HTML preparation code includes the necessary libraries (jQuery) and HTML structure for the benchmark. It sets up a container with multiple columns, each containing elements that will be targeted by the script preparation code.
Individual Test Cases
There are two individual test cases:
c1
: Targets elements with both an ID selector (#
) and a CSS class selector (articleContent h2
).c2
: Targets only elements with an ID selector (#maincontainer h2
).Comparison of Approaches
The benchmark compares the performance of these two approaches:
#maincontainer .articleContent h2
#maincontainer h2
Library: jQuery
The benchmark uses jQuery for its DOM manipulation and event handling. Specifically, the script preparation code uses $(this)
and .append()
methods from jQuery.
Other Considerations
Alternatives
If you're looking for alternative approaches, consider:
.articleContent h2
).document.querySelectorAll()
or Element.matches()
for more efficient and flexible DOM querying.