Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test if id selector is faster
(version: 0)
kekit
Comparing performance of:
c1 vs c2
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<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>
Script Preparation code:
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>'); });
Tests:
c1
$("#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>'); });
c2
$("#maincontainer h2").each(function() { $(this).attr('id', $(this).text().replace(/ /g,'-')); $(list).append('<li><a href="#' + $(this).text().replace(/ /g,'-') + '">' + $(this).text() + '</a></li>'); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
c1
c2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `#maincontainer .articleContent h2`: This approach targets elements with both an ID selector (`#`) and a CSS class selector (`articleContent`). 2. `#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: * Retrieves all elements with the ID `onThisPage` using jQuery. * Iterates over each element in `.articleContent h2` and: + Replaces spaces in the text with dashes (`-`) to create a unique ID. + Appends a new list item with an anchor tag containing the modified text. **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: 1. `c1`: Targets elements with both an ID selector (`#`) and a CSS class selector (`articleContent h2`). 2. `c2`: Targets only elements with an ID selector (`#maincontainer h2`). **Comparison of Approaches** The benchmark compares the performance of these two approaches: * **Approach 1: `#maincontainer .articleContent h2`** + Pros: - More specific targeting, which can lead to faster execution. - Less chance of matching other elements accidentally. + Cons: - May require more processing power due to the additional CSS class selector. * **Approach 2: `#maincontainer h2`** + Pros: - Simpler and faster targeting, as it only requires an ID selector. - Less processing power required. + Cons: - May match other elements accidentally, leading to slower execution. **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** * The benchmark uses a real-world HTML structure and content to make the results more relevant. * The test cases are designed to simulate common web development scenarios where ID selectors are used. **Alternatives** If you're looking for alternative approaches, consider: * Using CSS classes instead of IDs for targeting elements (e.g., `.articleContent h2`). * Leveraging modern JavaScript features like `document.querySelectorAll()` or `Element.matches()` for more efficient and flexible DOM querying. * Experimenting with different optimization techniques, such as caching or memoization, to improve performance.
Related benchmarks:
Selectors
jQuery vs vanilla JS in selector class
Test selectors
jQuery selectors efficiency on repeated use - v2
Comments
Confirm delete:
Do you really want to delete benchmark?