<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", i/100 + "%");
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:${i/100}%;`;
i++;
}
let i = 0;
while (i < 10000) {
el.style = `color:red;border:1vmin solid red;padding:0.5vmin;background-color:black;height:1vh;width:${i/100}%;`;
i++;
}
let i = 0;
while (i < 10000) {
Object.assign(el.style, {
height: '1vh',
width: i/100 + '%',
color: 'red',
border: '1vmin solid red',
backgroundColor: 'black',
padding: '0.5vmin'
});
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:${i/100}%;`);
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 = i/100 + "%";
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:${i/100}%;`;
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style replace | |
Object.assign | |
setAttribute | |
style update | |
style.cssText update |
Test name | Executions per second |
---|---|
style.setProperty | 27.9 Ops/sec |
style.cssText | 36.8 Ops/sec |
style replace | 32.8 Ops/sec |
Object.assign | 31.2 Ops/sec |
setAttribute | 33.0 Ops/sec |
style update | 25.9 Ops/sec |
style.cssText update | 12.7 Ops/sec |
Let's dive into the world of JavaScript benchmarks.
What is being tested?
MeasureThat.net is testing four different ways to set CSS properties on an HTML element: style.setProperty
, style.cssText
, Object.assign
(on the style object), and setAttribute
. The test cases aim to compare the performance, syntax complexity, and potential issues with each approach.
Options compared:
Pros and Cons:
color
and red
), not individual property names (e.g., color
alone).style.setProperty
.color
instead of color: red;
) and may not support multiple properties at once.Performance implications:
In general, style.setProperty
is likely to be the fastest option due to its optimized parsing and execution by modern browsers. However, the actual performance difference between these options might be negligible unless you're working with extremely large stylesheets or high-performance applications.
Conclusion:
The choice of method depends on your specific use case, personal preference, and project requirements. If you need to set multiple properties at once and prioritize ease of use, style.setProperty
is a solid choice. For simpler cases where individual property names are sufficient, style.cssText
might be the better option. Object.assign (on style object) and setAttribute
can be useful in specific situations but may require more setup or have limitations.
Keep in mind that these results are based on MeasureThat.net's test data, which might not reflect your specific use case or environment. If you're concerned about performance or have specific questions, consider benchmarking your own code or consulting with a web development expert.