Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setAttribute vs style.cssText test
(version: 1)
Comparing performance of:
setAttribute vs style
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Script Preparation code:
var element = document.getElementById("foo"); const array = new Array(10000); for (var i = 0; i < array.length; i++) { array[i] = `width: ${i}px; height: ${i}px;` }
Tests:
setAttribute
for (var i = 0; i < array.length; i++) { element.setAttribute("style", array[i]); }
style
for (var i = 0; i < array.length; i++) { element.style.cssText = array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
setAttribute
style
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setAttribute
22.5 Ops/sec
style
30.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark is designed to compare two methods for applying CSS styles to a DOM element in JavaScript: using the `setAttribute()` method and directly setting the `style.cssText` property. Here’s a breakdown of what is being tested, the differences between the methods, and considerations for each approach. ### Options Compared 1. **setAttribute Method** - **Test Case:** ```javascript for (var i = 0; i < array.length; i++) { element.setAttribute("style", array[i]); } ``` - **Description:** This method involves calling the `setAttribute()` function, which sets the value of the specified attribute (in this case, "style") on the DOM element. 2. **style.cssText Property** - **Test Case:** ```javascript for (var i = 0; i < array.length; i++) { element.style.cssText = array[i]; } ``` - **Description:** This method directly assigns a CSS style string to the `cssText` property of the element's `style` object. ### Pros and Cons #### setAttribute - **Pros:** - Can be used for setting any attribute of an element, not limited to styles. This makes it versatile. - Easier to use for cases where you might want to dynamically set multiple attributes at once. - **Cons:** - Performance-wise, `setAttribute` is typically slower than direct property access, especially when called repeatedly in a loop, because it may involve more overhead as it parses the attribute string. - Overwrites all existing inline styles when setting the "style" attribute, which could lead to unexpected results if multiple styles are defined. #### style.cssText - **Pros:** - Generally offers better performance for styling because it interacts directly with the style object of the element. - Allows for appending styles without overwriting other existing styles that are already set on the element. This makes it easier to modify only specific styles. - **Cons:** - Less flexible than `setAttribute` in terms of general attribute manipulation; it can only be used for CSS styles. - Requires careful construction of the style string to avoid syntax errors. ### Considerations When choosing between these two methods, performance is a crucial factor, particularly in cases where styles are changed frequently, such as within animations or user interactions. The results indicate that the `style.cssText` approach performed better in this benchmark, achieving more executions per second compared to `setAttribute`. This benchmark serves as a practical example for developers, illustrating the real-world impact of these two methods on performance. ### Alternatives Other alternatives for applying styles to elements in JavaScript include: - **Individual Style Properties:** Instead of setting the whole string, styles can be applied by setting individual properties on the `style` object, e.g., `element.style.width = '100px'; element.style.height = '100px';`. This can be even more performant in scenarios where you are changing a few styles without needing to reconstruct the entire style string. - **CSS Classes:** Instead of inline styles, one could add or remove classes from the element, managing styles through CSS classes rather than JavaScript. This is generally more performant and makes the code cleaner and easier to maintain, as styles are kept separate from the JavaScript logic. By understanding these techniques and their respective trade-offs, developers can make informed decisions about how to manipulate styles and optimize their application's performance.
Related benchmarks:
className vs. setAttribute vs. classList
setAttribute vs style
style➜display VS setAttribute➜display
Set Attribute
direct style vs. setAttribute vs. classList
2 direct styles vs. setAttribute vs. classList
Set className vs. setAttribute
className vs setAttribute vs classList
setAttribute vs style.cssText
Comments
Confirm delete:
Do you really want to delete benchmark?