Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
matches or getAttribute
(version: 0)
Comparing performance of:
matches vs getAttribute
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div data-foo="bar"></div>
Tests:
matches
var element = document.querySelector("div"); var i = 1000; while (i--) { console.log(element.matches("[data-foo]")); }
getAttribute
var element = document.querySelector("div"); var i = 1000; while (i--) { console.log(!!element.getAttribute("data-foo")); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
matches
getAttribute
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
matches
202.9 Ops/sec
getAttribute
207.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance differences between two approaches can be an interesting exercise. The provided JSON represents a benchmark test case that compares the performance of using `element.matches()` and `element.getAttribute("data-foo")` to check if an HTML element has a certain attribute. Let's break down what's being tested: **Benchmark Definition:** * The script preparation code is empty, which means no additional setup or dependencies are required for this test. * The HTML preparation code creates a simple `<div>` element with an `data-foo` attribute. The test case consists of two individual tests: 1. "matches" * This test uses `element.matches()` to check if the element has the `[data-foo]` pseudo-class. This is a CSS-based approach that doesn't require any explicit attribute value. 2. "getAttribute" * This test uses `element.getAttribute("data-foo")` to access and retrieve the value of the `data-foo` attribute. **Options Compared:** In this case, we're comparing two approaches: 1. **CSS-based matching (`element.matches()`)** 2. **Attribute-based checking (`element.getAttribute("data-foo")`)** **Pros and Cons of Each Approach:** 1. **CSS-based Matching (`element.matches()`):** * Pros: + Fast and efficient, as it uses a single DOM query. + No need to access the attribute value explicitly. * Cons: + Might not work in older browsers that don't support pseudo-classes. + Can be slower for elements with many attributes or complex selectors. 2. **Attribute-based Checking (`element.getAttribute("data-foo")`):** * Pros: + Works in all modern browsers, including older versions. + Allows access to the attribute value without needing a separate query. * Cons: + Slower than CSS-based matching due to an additional DOM lookup and string manipulation. **Library/Dependency Used:** There are no external libraries or dependencies mentioned in the benchmark definition. However, it's worth noting that `element.matches()` is a standard method introduced in HTML5, so most modern browsers should support it without any additional setup. **Special JS Feature/Syntax:** There is no special JavaScript feature or syntax used in this benchmark. The test cases are straightforward and use standard DOM methods. **Other Alternatives:** If you were to explore other alternatives for checking if an element has a certain attribute, you might consider: * Using the `getAttribute()` method with a default value, like this: `!!element.getAttribute("data-foo", "default-value")` * Utilizing a library like jQuery, which provides methods like `.attr()` and `.has()` * Implementing your own custom solution using DOM manipulation or regular expressions Keep in mind that these alternatives might introduce additional overhead or dependencies, so it's essential to consider the specific use case and performance requirements when choosing an approach.
Related benchmarks:
attr teststa
getAttribute('data-foo') vs dataset.foo
Class Selector vs AttributeSelector 2
Check data attribute: hasAttribute vs dataset
matches vs getattribute
Comments
Confirm delete:
Do you really want to delete benchmark?