<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 style = {
height: '1vh',
width: '1vw',
color: 'red',
border: '1vmin solid red',
backgroundColor: 'black',
padding: '0.5vmin'
};
let i = 0;
while (i < 10000) {
let cssText = Object.entries(style).reduce((acc, [key, value]) => {
acc += `${key}: ${value};`
return acc;
}, '');
el.style.cssText = cssText;
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style | |
Object.assign | |
setAttribute | |
Object.entries with style.cssText |
Test name | Executions per second |
---|---|
style.setProperty | 11.0 Ops/sec |
style.cssText | 20.5 Ops/sec |
style | 17.2 Ops/sec |
Object.assign | 9.5 Ops/sec |
setAttribute | 19.0 Ops/sec |
Object.entries with style.cssText | 13.6 Ops/sec |
I'll break down the explanation into smaller parts to make it easier to understand.
Benchmark Overview
The benchmark measures the performance of different ways to set CSS styles on an HTML element in JavaScript. The test cases compare six approaches:
style.setProperty
style.cssText
style
Object.assign
setAttribute
Object.entries with style.cssText
Approach 1: style.setProperty
style.setProperty
is a DOM method that allows you to set CSS properties on an element without having to create a new stylesheet or manipulate the existing one. It takes two arguments: the property name and its value.
Pros:
Cons:
Approach 2: style.cssText
style.cssText
is a property of the style
object that allows you to set a single CSS text value. You can use it by setting a string with multiple styles separated by semicolons (e.g., "color:red;border:1vmin solid red;"
).
Pros:
Cons:
Approach 3: style
Setting style
directly on an element is another way to apply CSS styles. You can pass a string with multiple styles separated by semicolons (e.g., "color:red;border:1vmin solid red;"
).
Pros:
style.cssText
, easy to implement and understand.Cons:
Approach 4: Object.assign
Object.assign
is a method that allows you to copy properties from one object to another. In this case, it's used to set the style
property of an element by passing an object with CSS styles.
Pros:
Cons:
Approach 5: attributeSet
setAttribute
is a method that allows you to set an attribute on an element. In this case, it's used to set CSS styles by passing the property name and value as arguments.
Pros:
Cons:
Approach 6: Object.entries with style.cssText
This approach combines Object.entries
(which returns an array of key-value pairs from an object) with style.cssText
. The resulting string is then applied to the element's style
property using style.cssText
.
Pros:
Cons:
Benchmark Results
The benchmark results show that style.cssText
consistently performs better than the other approaches, likely due to its simplicity and minimal DOM overhead. However, the performance differences between the approaches are relatively small, indicating that most of the difference lies in the implementation details rather than the fundamental approach itself.
Keep in mind that this is a simplified explanation, and there may be additional factors that influence the performance of each approach depending on your specific use case.