Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hasAttribute with/out getAttribute
(version: 1)
Comparing performance of:
with getAttribute vs without getAttribute vs forOf (with getAttribute) vs forOf (without getAttribute)
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="div1" class=""> <div id="div2" class="test"> <div id="div3"> <div id="div4" class="test"> <div id="div5" class="test test2"> </div> <script> Benchmark.prototype.setup = function() { const nodes = document.querySelectorAll('div'); }; </script>
Tests:
with getAttribute
let list = []; nodes.forEach((node) => { if(node && node.hasAttribute && node.hasAttribute("class") && node.getAttribute("class")) { list.push(node); } }) console.log(list)
without getAttribute
let list = []; nodes.forEach((node) => { if(node && node.hasAttribute && node.hasAttribute("class")) { list.push(node); } }) console.log(list)
forOf (with getAttribute)
let list = []; for(const node of nodes) { if(node && node.hasAttribute && node.hasAttribute("class") && node.getAttribute("class")) { list.push(node); } } console.log(list)
forOf (without getAttribute)
let list = []; for(const node of nodes) { if(node && node.hasAttribute && node.hasAttribute("class")) { list.push(node); } } console.log(list)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
with getAttribute
without getAttribute
forOf (with getAttribute)
forOf (without 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):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance difference between three different approaches for checking if an HTML element has a specific attribute: `getAttribute`, `hasAttribute` with and without the `class` attribute. **Options Compared** 1. **with getAttribute**: This approach checks if the element has both `hasAttribute` and `getAttribute` methods available, and then calls `getAttribute` on the element to check for the presence of the `class` attribute. 2. **without getAttribute**: This approach only checks if the element has the `hasAttribute` method available, without calling it. 3. **forOf (with getAttribute)**: This approach uses a `for...of` loop to iterate over the elements, and inside the loop, it calls `hasAttribute` on each element to check for the presence of the `class` attribute. 4. **forOf (without getAttribute)**: Similar to the previous one, but without calling `hasAttribute`. **Pros and Cons** * **with getAttribute**: This approach is more accurate because it checks both methods. However, it's slower due to the additional method call. * **without getAttribute**: This approach is faster but less accurate because it only checks for the presence of the `hasAttribute` method. * **forOf (with getAttribute)** and **forOf (without getAttribute)**: These approaches are more concise and efficient, but may not be as accurate as the first two options. **Library Used** In this benchmark, `document.querySelectorAll` is used to select all elements on the page. This is a standard JavaScript library that allows you to query the DOM for elements using CSS selectors. **Special JS Feature/Syntax** None mentioned in this specific benchmark. However, it's worth noting that some modern browsers support newer JavaScript features like `for...of`, which is used in two of these options. **Other Alternatives** Other approaches could include: * Using a library like jQuery to simplify the DOM manipulation and attribute checking. * Using a different method for selecting elements, such as `document.getElementsByClassName` or `querySelectorAll` with a more specific selector. * Adding additional checks or optimizations, such as caching the results of `hasAttribute` calls. Overall, this benchmark provides a useful comparison of different approaches to checking if an HTML element has a specific attribute, and can help developers choose the most efficient method for their use case.
Related benchmarks:
classList.contains vs. hasAttribute
getAttribute/setAttribute versus classList
className + attribute vs attribute + className
js - title vs getAttribute vs hasAttribute /well
js - title vs getAttribute vs hasAttribute /well2
Comments
Confirm delete:
Do you really want to delete benchmark?