Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof + className vs. getAttribute for SVGs and normal elements
(version: 1)
Comparing performance of:
getAttribute only vs ternary typeof check to determine getAttribute or className
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="foo"></div> <svg id="bar" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 20 20" aria-label="Search Icon"><path fill="" d="M14.556019 15.61668c-3.437834 2.899823-8.583187 2.73052-11.82163-.507923-3.417087-3.417086-3.417088-8.95728 0-12.374368 3.417087-3.417088 8.957282-3.417087 12.374368 0 3.238443 3.238443 3.407746 8.383796.507922 11.82163l5.148932 5.148932c.292891.292891.292894.767767 0 1.06066-.292893.292894-.767769.292891-1.06066 0L14.55602 15.61668zm-.507922-1.568583c2.8313-2.8313 2.831298-7.42175 0-10.253048-2.831298-2.831298-7.421747-2.831301-10.253048 0-2.831301 2.8313-2.831298 7.42175 0 10.253048 2.831298 2.831298 7.421747 2.8313 10.253048 0z"></path></svg>
Tests:
getAttribute only
var element = document.getElementById("foo"); var element2 = document.getElementById("bar"); var class_name; var i = 1000; while (i--) { if (i % 2 === 0) { class_name = element.getAttribute("class"); } else { class_name = element2.getAttribute("class"); } }
ternary typeof check to determine getAttribute or className
var element = document.getElementById("foo"); var element2 = document.getElementById("bar"); var class_name; var i = 1000; while (i--) { if (i % 2 === 0) { class_name = typeof element.className === "object" ? element.getAttribute("class") : element.className; } else { class_name = typeof element2.className === "object" ? element2.getAttribute("class") : element2.className; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getAttribute only
ternary typeof check to determine getAttribute or className
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case, specifically comparing the performance of two approaches for accessing class names in HTML elements: using `getAttribute` and using `typeof` with conditional logic. **Test Cases** There are two individual test cases: 1. **"getAttribute only"`**: This test case measures the performance of simply calling `getAttribute` on an element's `class` attribute. 2. **"ternary typeof check to determine getAttribute or className"`**: This test case measures the performance of using a ternary operator with `typeof` to determine whether to call `getAttribute` or access the `className` property directly. **Library and Special Features** The benchmark uses JavaScript's built-in DOM API to interact with HTML elements. No external libraries are required for this benchmark. There is no special JavaScript feature or syntax being tested in this benchmark. The focus is solely on comparing two different approaches to accessing class names in HTML elements. **Approach Comparison** Here's a brief explanation of each approach: 1. **`getAttribute`**: This method retrieves the value of a specified attribute for an element, including `class`. It returns a string containing the attribute's value(s), separated by spaces. 2. **Ternary `typeof` check with `getAttribute` or `className`**: * If the element's `className` property is an object (i.e., not null or undefined), it calls `getAttribute("class")`. * Otherwise, it accesses the `className` property directly. **Pros and Cons** **`getAttribute`**: Pros: * Easy to implement and understand * Works for all elements, regardless of their class names Cons: * May be slower due to attribute lookup (depending on browser implementation) * Returns a string containing multiple class names separated by spaces **Ternary `typeof` check with `getAttribute` or `className`**: Pros: * More efficient than using `getAttribute` since it avoids additional attribute lookups * Can provide better performance in cases where the element's `className` property is an object (i.e., has a nested class structure) Cons: * Requires understanding of JavaScript syntax and conditional logic * May be slower for certain elements or browsers due to the ternary operator evaluation **Other Considerations** When comparing these two approaches, it's essential to consider factors like: * Browser implementation: Different browsers may optimize `getAttribute` or the ternary operator differently. * Element class names: The structure and number of class names can impact performance. For example, if an element has many class names separated by spaces, `getAttribute` might be slower than using a more efficient approach. * Context: This benchmark measures pure JavaScript execution time; however, in real-world scenarios, you may need to consider other factors like DOM manipulation, event handling, or rendering. **Alternatives** If you're interested in exploring alternative approaches for accessing class names, here are some options: 1. **`classList` API**: Many modern browsers support the `classList` property, which provides a more efficient way to access an element's class names. 2. **Template literals**: You can use template literals to create a string containing the class name(s), which might be faster than using `getAttribute`. 3. **Native DOM methods**: Some elements provide native methods for accessing their class names, such as `querySelector` or `querySelectorAll`.
Related benchmarks:
Classnames vs CLSX vs Alternatives
Classnames vs CLSX vs Others
Classnames vs CLSX vs Alternatives vs custom
another classnames vs clsx
inline svg vs use
Comments
Confirm delete:
Do you really want to delete benchmark?