<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.style.color = "red";
el.style.border = "1vmin solid red";
el.style.padding = "0.5vmin";
el.style.backgroundColor = "black";
el.style.height = "1vh";
el.style.width = "1vw";
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style | |
Object.assign | |
setAttribute | |
Assign property |
Test name | Executions per second |
---|---|
style.setProperty | 5.3 Ops/sec |
style.cssText | 15.7 Ops/sec |
style | 12.8 Ops/sec |
Object.assign | 4.6 Ops/sec |
setAttribute | 362.1 Ops/sec |
Assign property | 5.5 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
The benchmark measures the performance of different ways to set CSS styles on an HTML element. The test compares six approaches:
style.setProperty
style.cssText
style
(assigning a string value directly)Object.assign
(using the object assignment method)setAttribute
(setting the style
attribute manually)Assign property
(directly assigning individual style properties)What is being tested?
The benchmark tests the performance of setting CSS styles on an HTML element by iterating over a large number of iterations (10,000). The test measures the time taken to execute each approach.
Options Compared
The benchmark compares six approaches:
style.setProperty
: uses the setProperty
method to set multiple CSS properties in a single call.style.cssText
: sets all CSS properties using the cssText
property.style
: assigns a string value directly to the style
property, setting all CSS properties at once.Object.assign
: uses the object assignment method (Object.assign
) to set multiple CSS properties in a single call.setAttribute
: sets the style
attribute manually using the setAttribute
method.Assign property
: directly assigns individual style properties (e.g., el.style.color = "red"
).Pros and Cons
Here's a brief summary of each approach:
Conclusion
The benchmark results will likely show that style.setProperty
and Object.assign
are the most efficient approaches, followed closely by setAttribute
. However, the actual performance differences between these approaches may vary depending on specific use cases and requirements.