<div id="test"></div>
el = document.getElementById("test");
let i = 0;
while (i < 10000) {
el.style.setProperty("color","red");
el.style.setProperty("border","1vmin solid red");
el.style.setProperty("padding","0.5vmin");
el.style.setProperty("background-color","black");
el.style.setProperty("height","1vh");
el.style.setProperty("width","1vw");
i++;
}
let i = 0;
while (i < 10000) {
el.style.cssText = "color:red;border:1vmin solid red;padding:0.5vmin;background-color:black;height:1vh;width:1vw;";
i++;
}
let i = 0;
while (i < 10000) {
el.style = "color:red;border:1vmin solid red;padding:0.5vmin;background-color:black;height:1vh;width:1vw;";
i++;
}
let style = {
height: '1vh',
width: '1vw',
color: 'red',
border: '1vmin solid red',
backgroundColor: 'black',
padding: '0.5vmin'
};
let i = 0;
while (i < 10000) {
Object.assign(el.style, style);
i++;
}
let i = 0;
while (i < 10000) {
el.setAttribute('style',"color:red;border:1vmin solid red;padding:0.5vmin;background-color:black;height:1vh;width:1vw;");
i++;
}
let i = 0;
while (i < 10000) {
el.attributeStyleMap.set("color","red");
el.attributeStyleMap.set("border","1vmin solid red");
el.attributeStyleMap.set("padding","0.5vmin");
el.attributeStyleMap.set("background-color","black");
el.attributeStyleMap.set("height","1vh");
el.attributeStyleMap.set("width","1vw");
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style | |
Object.assign | |
setAttribute | |
attributeStyleMap.set |
Test name | Executions per second |
---|---|
style.setProperty | 14.0 Ops/sec |
style.cssText | 12.5 Ops/sec |
style | 11.4 Ops/sec |
Object.assign | 12.8 Ops/sec |
setAttribute | 165.2 Ops/sec |
attributeStyleMap.set | 4.7 Ops/sec |
Let's dive into the JavaScript microbenchmarking provided by MeasureThat.net.
Benchmark Overview
The benchmark compares six different approaches to setting CSS styles on an HTML element:
style.setProperty()
style.cssText
style
Object.assign()
with the style objectsetAttribute()
with the style attributeattributeStyleMap.set()
with the attribute style mapComparison of Approaches
Here's a brief overview of each approach, along with their pros and cons:
style.setProperty()
: This method sets CSS properties on an element. It's a built-in method that allows for more flexibility in setting styles.style.cssText
: This property sets the entire style string for an element. It's a simple way to set styles, but it can be less efficient than other methods.style
: This is a shorthand property that sets multiple styles in a single string. It's similar to style.cssText
, but more limited in its capabilities.Object.assign()
with style object: This method uses the Object.assign()
function to copy properties from one object (in this case, the style object) to another element's style property.setAttribute()
with style attribute: This method sets the style
attribute of an HTML element using the setAttribute()
function.attributeStyleMap.set()
with attribute style map: This method uses a custom attribute called data-styles
to store CSS styles on an element.Benchmark Results
The benchmark results show the execution speed of each approach for different browsers (Chrome 112) on a desktop platform. The results suggest that:
style.setProperty()
is generally the fastest approach, followed closely by Object.assign()
with the style object.style.cssText
and style
are slower due to their simplicity and limited capabilities.setAttribute()
with the style attribute is significantly slower than other methods.attributeStyleMap.set()
with the attribute style map is the slowest approach, likely due to its custom implementation.In conclusion, while all approaches have their pros and cons, style.setProperty()
appears to be the most efficient and flexible method for setting CSS styles on an HTML element. However, the best approach ultimately depends on your specific use case, performance requirements, and personal preference.