Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JQuery: test find by tag vs find by class for non-document element
(version: 0)
check which selector is better for non-document element
Comparing performance of:
find by tag (if it possible) vs find by class
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js'></script>
Script Preparation code:
var element = $(`<div><a id="tr_1" class="c-1"> <svg id="tr_2" class="c-2"></svg> <span id="tr_3" class="c-3"></span> <svg></svg> <div id="tr_4" class="c-4"> </div> </div>`);
Tests:
find by tag (if it possible)
element.find("a"); element.find("#tr_2"); element.find("span"); element.find("div");
find by class
element.find("c-1"); element.find("c-2"); element.find("c-3"); element.find("c-4");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find by tag (if it possible)
find by class
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 dive into the explanation of what is being tested in this benchmark. **Benchmark Overview** The benchmark measures the performance difference between two ways to select elements in jQuery: `find()` by tag name and `find()` by class name, specifically when no document element is present. **Options Compared** Two options are compared: 1. **`element.find("a");`**: This option uses the `find()` method with a tag name selector (`"a"`). The idea behind this approach is to find an element with the `a` tag name within the DOM. 2. **`element.find("#tr_1");` and other class selectors**: This option uses the `find()` method with class name selectors (`"c-1"`, `"c-2"`, etc.). The idea behind this approach is to find an element with any of the specified classes. **Pros and Cons** The pros of using `find()` by tag name are: * Simplicity: This approach is straightforward and easy to understand. * Flexibility: Tag names can be more flexible than class names, as they don't require a specific naming convention. However, there are some cons: * Performance: When no document element is present, the browser needs to search through the entire DOM to find an element with the specified tag name. This can lead to slower performance. * Browser behavior: Different browsers may behave differently when using `find()` by tag name, which can affect performance. The pros of using `find()` by class name are: * Efficiency: When no document element is present, the browser only needs to search through elements with the specified classes. This can lead to faster performance. * Consistency: Browsers tend to behave more consistently when using class name selectors. However, there are some cons: * Complexity: Class name selectors can be more complex and harder to understand than tag name selectors. * Limitations: Class names require a specific naming convention (e.g., `c-1`, `c-2`, etc.), which can limit flexibility. **Library** The benchmark uses the jQuery library, which is a popular JavaScript library for DOM manipulation and event handling. The `find()` method is a core part of the jQuery API, allowing developers to select elements based on various criteria. **Special JS Feature or Syntax** There are no special JS features or syntax used in this benchmark, as it primarily focuses on comparing two simple selection methods within the jQuery library. **Other Alternatives** If you're interested in exploring other alternatives, here are a few options: * **`document.querySelector()`**: This method is similar to `find()` but uses a CSS selector instead of a tag name or class name. It's more flexible and powerful than `find()`, but can be slower for simple use cases. * **`querySelectorAll()`**: This method returns an array of elements that match the specified CSS selector. It's similar to `document.querySelector()`, but returns all matches instead of just one. Keep in mind that these alternatives may have different performance characteristics and require more expertise to use effectively.
Related benchmarks:
JQuery: find by class vs find by tag vs children
JQuery: test find by id and find by id and tag for non-document element
querySelectorAll: id vs tag vs class
JQuery: find with id vs find with tag name vs global id selector
Comments
Confirm delete:
Do you really want to delete benchmark?