Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JQuery: hidden selector vs hidden attribute v2
(version: 1)
find the best solution for get hidden element
Comparing performance of:
using hidden selector vs using hidden attribute
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<select id="items"> <option value="1" disable hidden></option> <option value="2"></option> <option value="3"></option> <option value="4"></option> <option value="5"></option> <option value="6" disable hidden></option> <option value="7"></option> <option value="8"></option> <option value="9"></option> <option value="10"></option> </select> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js'></script>
Script Preparation code:
var $itemsElement = $("#items");
Tests:
using hidden selector
$itemsElement.find("option:hidden:disabled");
using hidden attribute
$itemsElement.find("option[hidden]:disabled");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
using hidden selector
using hidden attribute
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 and explain what is being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for selecting an element with a specific attribute in jQuery: using a hidden selector (`:hidden`) versus using a hidden attribute (`[hidden]`). The goal is to determine which approach is faster. **Options Compared** Two options are compared: 1. **Hidden Selector**: `$( "#items" ).find( "option:hidden:disabled" );` * Pros: + More concise and readable syntax. + May be more efficient for certain use cases (e.g., when the attribute is not present, the browser can skip checking). * Cons: + Requires jQuery 1.9 or later, which may not be supported by all browsers. + Can be slower in older browsers that do not support this syntax. 2. **Hidden Attribute**: `$( "#items" ).find( "option[hidden]:disabled" );` * Pros: + More widely supported across different browsers and versions. + May be faster in older browsers that do not support the hidden selector syntax. * Cons: + Less concise and less readable syntax. **Library and Purpose** The jQuery library is used to simplify DOM manipulation. In this benchmark, it provides a convenient way to select elements based on their attributes. **Special JS Feature or Syntax** None of the test cases use any special JavaScript features or syntax that would affect the comparison between the two approaches. **Other Considerations** When evaluating these options, consider factors such as: * Browser support: Which browsers support the hidden selector syntax? Are there differences in performance across different browsers? * Attribute presence: How do the two approaches handle cases where the attribute is not present on the element? * Readability and maintainability: Which approach is more readable and easier to understand for developers? **Alternative Approaches** If you're interested in exploring alternative approaches, consider: * Using `$( "#items" ).filter( function() { return this.getAttribute("hidden") == "disabled"; } );`: This approach uses the `getAttribute` method to check the attribute value. However, it may be slower due to the additional function call and attribute access. * Using a custom function or a third-party library that supports more advanced selectors: These approaches might provide better performance but also add complexity and dependencies. Keep in mind that these alternatives are not directly comparable to the original hidden selector and hidden attribute approaches.
Related benchmarks:
JQuery: compare approaches for getting first option
JQuery: compare approaches for get of children
JQuery: find elements which does not need have value vs not function vs not selector
JQuery: find with selected selector vs filter selected selector
Comments
Confirm delete:
Do you really want to delete benchmark?