Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set Prop Variable vs setAttribute
(version: 0)
Comparing performance of:
style.setProperty vs style.setAttribute
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<style> .id { background-color: var(--myVar); } </style> <div id="test">Some content here</div>
Script Preparation code:
element = document.getElementById("test");
Tests:
style.setProperty
let i = 0; while (i < 10000) { element.style.setProperty("--myVar", "blue"); element.style.setProperty("--myVar", "red"); i++; }
style.setAttribute
let i = 0; while (i < 10000) { element.setAttribute("style", "--myVar: blue"); element.setAttribute("style", "--myVar: red"); i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
style.setProperty
style.setAttribute
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
style.setProperty
121.7 Ops/sec
style.setAttribute
95.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases to help you understand what's being tested. **Benchmark Definition:** The benchmark definition represents a JavaScript microbenchmark that compares two approaches for setting a CSS property value on an HTML element: 1. `setProperty`: Using the `style.setProperty()` method. 2. `setAttribute`: Using the `element.setAttribute()` method with a string containing the CSS property-value pair. **Options Compared:** * `setProperty` vs `setAttribute` The benchmark is testing which approach is faster and more efficient for setting a CSS property value on an HTML element, specifically when setting multiple values in a loop. **Pros and Cons of Each Approach:** 1. `setProperty`: * Pros: + More concise and readable syntax. + Less chance of errors due to incorrect syntax or formatting. + Better support for CSS variables (e.g., `var(--myVar)`). * Cons: + May have performance overhead due to parsing and executing the string argument. 2. `setAttribute`: * Pros: + Can be more efficient when setting multiple values, as it avoids string concatenation or formatting issues. + Supports CSS variables (e.g., `var(--myVar)`). * Cons: + More verbose syntax can lead to errors or mistakes. + May have performance overhead due to parsing and executing the string argument. **Library Usage:** The benchmark uses no external libraries. It relies solely on built-in JavaScript methods (`style.setProperty()` and `element.setAttribute()`) and DOM elements. **Special JS Features/Syntax:** None mentioned in this specific benchmark definition. However, it's worth noting that some modern browsers (e.g., Chrome) support CSS variables (e.g., `var(--myVar)`), which might be relevant for other benchmarks or tests. **Other Alternatives:** * Other methods for setting CSS property values could include: + Using a stylesheet or CSS rules (`element.style.cssText`). + Using the `DOMTokenList` API (`element.style.setProperty()` with `tokenList` option). + Using a third-party library or framework (e.g., React's `useStyle` hook). Keep in mind that this benchmark is focused on comparing two specific approaches, and other alternatives might be relevant for different use cases or requirements.
Related benchmarks:
style➜display/backgroundColor VS setAttribute➜display/backgroundColor
Setting CSS: Direct CSS property vs setAttribute("style",...)
Set Prop Varaible vs setAttribute
6 properties - style.setProperty vs style.cssText vs style vs Object.assign vs setAttribute 2
Comments
Confirm delete:
Do you really want to delete benchmark?