Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test wildcard selectors with and without tag
(version: 1)
Comparing performance of:
Directly vs Via attr
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"> </script> <div> <ul> <li> <a href="#" name="name" class="classname">item 1</a> </li> <li> <a href="#" name="name" class="classname">item 2</a> </li> <li> <a href="#" name="name" class="classname">item 3</a> </li> <li> <a href="#" name="name" class="classname">item 4</a> </li> <li> <a href="#" name="name" class="classname">item 5</a> </li> <li> <a href="#" name="name" class="classname">item 6</a> </li> <li> <a href="#" name="name" class="classname">item 7</a> </li> <li> <a href="#" name="name" class="classname">item 8</a> </li> <li> <a href="#" name="name" class="classname">item 9</a> </li> <li id="last"> <a href="#" name="name" class="classname">item 10</a> </li> </ul> </div>
Script Preparation code:
var node;
Tests:
Directly
node = $('[class*="classname"]');
Via attr
node = $('a[class*="classname"');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Directly
Via attr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Directly
448128.6 Ops/sec
Via attr
440880.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark JSON focuses on testing the performance of two different jQuery selector approaches for selecting DOM elements. The specific case involves selecting anchors (`<a>`) based on their class names. ### Options Compared 1. **Direct Selection**: - *Benchmark Definition*: `node = $('[class*="classname"]');` - *Test Name*: "Directly" - This approach uses a wildcard attribute selector that selects all elements whose class attribute contains the substring "classname". 2. **Attribute Based Selection**: - *Benchmark Definition*: `node = $('a[class*="classname"]');` - *Test Name*: "Via attr" - This method specifies both the tag (`<a>`) and the class with a wildcard selector, limiting the selection to anchor elements that have the class "classname". ### Performance Results - **Direct Selection**: - Executions per second: 448,128.625 - **Via attr Selection**: - Executions per second: 440,880.84375 The results indicate that the direct selection method is slightly faster than the attribute-based selection method, showcasing a performance difference of approximately 7,000 executions per second in favor of direct selection. ### Pros and Cons - **Direct Selection**: - **Pros**: - Simpler and more concise syntax. - Faster performance in terms of execution speed. - **Cons**: - Less specific; may select elements that are not needed if there are other classes or elements present, which could lead to handling extra DOM elements. - **Attribute Based Selection**: - **Pros**: - More specific and targeted, as it only selects `<a>` elements. - Reduces potential overhead of manipulating unnecessary elements if multiple types of elements share the same class. - **Cons**: - Slightly slower performance due to additional constraints on selection. ### Other Considerations The performance difference may not be significant in many real-world applications where the number of affected elements is relatively small. However, in scenarios involving large DOM trees or frequent selection operations, the difference may have more noticeable effects. ### Alternative Approaches 1. **Using Vanilla JavaScript**: - Instead of jQuery's syntax, one could use `querySelectorAll`: ```javascript var node = document.querySelectorAll('.classname'); // Similar to Direct Selection var node = document.querySelectorAll('a.classname'); // Similar to Via attr ``` - This would lead to performance differences based on the native implementation of the browser, which may be more efficient due to no library overhead. 2. **Using Document Fragment or Caching**: - If selections are frequently reused, storing references in variables or utilizing a Document Fragment can enhance performance by avoiding repeated queries. 3. **Avoiding Wildcard Selectors**: - Limiting the selector to specific classes or attributes (e.g., using `class="classname"` directly) can maximize performance but at the cost of flexibility in selection. By understanding the testing approach detailed in the benchmark, developers can make informed decisions on selecting DOM elements, balancing performance with specificity and ease of maintenance.
Related benchmarks:
Selectors
jQuery vs vanilla JS in selector class
attribute selector
querySelector vs. getElementsByClassName[0]
jQuery remove vs hide
jQuery `tag.class` vs `.class`
Test selectors
jQuery selectors efficiency on repeated use - v2
Test wildcard selectors with and without tag 2
Comments
Confirm delete:
Do you really want to delete benchmark?