Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
adasdasd
(version: 0)
asdas
Comparing performance of:
classlist contains vs getAttribute
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="test" class="xr-template-disabled visable" interact="interactable" another="yes"/>
Tests:
classlist contains
let test = document.getElementById("test"); let runs = 10000; for (i= 0; i < runs; i++) { test.classList.contains("xr-template-disabled"); }
getAttribute
let test = document.getElementById("test"); let runs = 10000; for (i= 0; i < runs; i++) { test.getAttribute("interactable"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
classlist contains
getAttribute
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):
I'll break down the benchmark and explain what's being tested, compared, and other considerations. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark on MeasureThat.net. The benchmark definition consists of: 1. `Name` and `Description`: These fields provide metadata about the benchmark. 2. `Script Preparation Code` and `Html Preparation Code`: These fields are empty in this case, which means that no script or HTML code needs to be executed before running the benchmark. **Individual Test Cases** There are two test cases: 1. **Classlist Contains** * Benchmark Definition: This script gets an element by its ID, then checks if the `xr-template-disabled` class is present on the element. * Purpose: This test case measures how fast the browser can execute a simple DOM query and class check. 2. **getAttribute** * Benchmark Definition: Similar to the previous test case, but instead of checking a class, it retrieves an attribute value (in this case, `interactable`) from the same element. **Comparison** The two test cases compare the performance of different approaches: 1. Checking if a class is present using `classList.contains()`. 2. Retrieving an attribute value using `getAttribute()`. **Pros and Cons** 1. **`classList.contains()`**: * Pros: Fast, efficient, and widely supported. * Cons: May be slower than pure property access (attribute retrieval) in some cases, as it involves a DOM traversal. 2. **`getAttribute()`**: * Pros: Pure property access is often faster, especially for simple attribute retrievals. * Cons: Not all browsers support the `getAttribute()` method consistently; older browsers might return a different value. **Library and Special JS Features** There are no explicit libraries used in these test cases. However, some modern JavaScript features like ES6 classes (not used in this case) or template literals (used in the HTML preparation code) might be relevant for more complex benchmarks. **Other Considerations** * **DOM Traversal**: The `classList.contains()` method involves traversing the DOM to find the element and checking its class list. This can add overhead compared to pure attribute access. * **Browser Consistency**: Different browsers might handle attribute retrieval differently, which could impact benchmark results. * **Cache and Optimization**: Browsers often cache frequently accessed elements or attributes, which can affect performance in certain cases. **Alternatives** Other alternatives for measuring class list checks or attribute retrievals include: 1. Using the `document.querySelector()` method to retrieve an element by its ID or a CSS selector. 2. Employing more advanced techniques like using Web Workers or optimizing DOM traversal. 3. Using benchmarking frameworks like Benchmark.js or WebPerf. Keep in mind that these alternatives might not be directly applicable to this specific benchmark, as the focus is on measuring simple class list checks and attribute retrievals.
Related benchmarks:
IndexOfAll - Reduce vs For
cdfersd
lodash merge vs object.assign vs spread 3
lodash merge vs object.assign vs spread 4
lodash vs radash 3
Comments
Confirm delete:
Do you really want to delete benchmark?