<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++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style | |
Object.assign | |
setAttribute |
Test name | Executions per second |
---|---|
style.setProperty | 35.3 Ops/sec |
style.cssText | 33.4 Ops/sec |
style | 29.6 Ops/sec |
Object.assign | 32.6 Ops/sec |
setAttribute | 766.6 Ops/sec |
Let's dive into the benchmark and explore what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of six different ways to set CSS styles on an HTML element:
style.setProperty
style.cssText
style
Object.assign
with a style objectsetAttribute
with a style stringEach test case uses a similar setup: a container element (el
) is created, and then a loop of 10,000 iterations sets the styles using one of the six methods.
Options Compared
Here's a brief overview of each option:
color
, border
, etc.) separately. It's considered more efficient because it only updates the specific property being changed, whereas other methods update all styles at once.style.cssText
, but without the string syntax. It still updates all styles at once, making it less efficient.Object.assign
function to update an element's style object. While this approach is more efficient than updating individual properties, it requires creating and manipulating objects, which can be slower than direct property updates.style
attribute of an element using a string that contains all the styles. Similar to style.cssText
, but without the need for parsing; however, this approach is less efficient because it requires updating the entire style string.Pros and Cons
Here's a brief summary of each option:
style.cssText
.Other Considerations
When comparing these options, it's essential to consider factors like:
style.setProperty
are often preferred for their ease of use.Alternatives
If you're interested in exploring alternative approaches, consider:
CSSStyleDeclaration
API or the style.set()
method, which offer improved performance and flexibility.Keep in mind that the results of this benchmark might not be representative for all use cases. The choice of method ultimately depends on your specific requirements, performance constraints, and coding style preferences.