Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setAttribute vs style.cssText
(version: 0)
Comparing performance of:
setAttribute vs style
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
setAttribute
var element = document.getElementById("foo"); var i = 10000; while (i--) { element.setAttribute("style", "width: "+i+"px; height: "+i+"px;"); }
style
var element = document.getElementById("foo"); var i = 10000; while (i--) { element.style.cssText = `width: ${i}px; height: ${i}px;`; }
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:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setAttribute
193.2 Ops/sec
style
240.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of setting attribute and styling an element using CSS are two common scenarios that can be compared to understand which approach is faster. **Setting Attribute vs Styling with CSS** The two approaches being compared in this benchmark are: 1. **setAttribute**: This method involves setting the `style` attribute of the element directly. The syntax is `element.setAttribute('style', 'width: ' + i + 'px; height: ' + i + 'px');`. This approach is more prone to browser quirks and inconsistencies because it relies on the browser's interpretation of the CSS syntax in the `style` attribute. 2. **Styling with CSS**: This method involves setting the `cssText` property of the element using a template literal. The syntax is `element.style.cssText = 'width: ' + i + 'px; height: ' + i + 'px';`. This approach is more consistent and reliable because it follows the standard CSS syntax. **Pros and Cons** * **setAttribute**: + Pros: Can be faster in some cases due to the simplicity of the operation. + Cons: More prone to browser quirks, may not work as expected in all browsers, and can lead to inconsistent results. * **Styling with CSS**: + Pros: More consistent and reliable across different browsers, follows standard CSS syntax. + Cons: May be slower than `setAttribute` due to the overhead of parsing and executing the CSS code. **Library** There is no library being used in this benchmark. The JavaScript code is simple and does not involve any external libraries or frameworks. **Special JS Feature/Syntax** The test case uses a template literal feature in JavaScript, which allows you to embed expressions inside string literals using the backtick (`) character. This feature was introduced in ECMAScript 2015 (ES6) and has been widely adopted since then. **Other Alternatives** If you were to implement this benchmark yourself, you could consider other approaches such as: * Using a CSS-in-JS library like styled-components or emotion. * Utilizing a CSS preprocessing tool like PostCSS or Sass. * Implementing a custom CSS parser and renderer. However, these alternatives would likely introduce additional complexity and overhead, making them less suitable for a simple benchmarking scenario like this one.
Related benchmarks:
style➜display VS setAttribute➜display
style➜display VS setAttribute➜display
2 direct styles vs. setAttribute vs. classList
Setting CSS: Direct CSS property vs setAttribute("style",...)
Comments
Confirm delete:
Do you really want to delete benchmark?