<style>
:root {
--test-color: 'red';
}
.test {
color: var(--test-color);
}
.test-blue {
--test-color: 'blue';
}
</style>
<div id="test"></div>
el = document.getElementById("test");
let i = 0;
while (i < 10000) {
el.style.setProperty("--test-color","blue");
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.classList.add('test-blue');
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style | |
Object.assign | |
setAttribute | |
classList |
Test name | Executions per second |
---|---|
style.setProperty | 408.5 Ops/sec |
style.cssText | 102.4 Ops/sec |
style | 92.9 Ops/sec |
Object.assign | 89.1 Ops/sec |
setAttribute | 1036.7 Ops/sec |
classList | 806.8 Ops/sec |
This benchmark on MeasureThat.net evaluates various approaches for applying CSS styles to a DOM element in JavaScript. Specifically, it compares six different methods for changing the styles of an HTML element repeatedly within a loop of 10,000 iterations. Each method is tested to determine how performant it is in terms of executions per second.
style.setProperty
style.cssText
cssText
, you can override all the styles of the element.style (direct assignment)
cssText
.cssText
, but lacks the performance due to repeated reassignment of the style object as a whole.Object.assign
setProperty
or cssText
.setAttribute
classList (add)
From the benchmark results provided:
setAttribute
executed at approximately 1036 executions per second.classList
(around 807 executions per second) and style.setProperty
(around 408 executions per second).style.cssText
(around 102 executions per second), style
(around 92 executions per second), and Object.assign
(around 89 executions per second).Performance Impact: The choice of styling method has a significant impact on performance, particularly in scenarios involving frequent updates (like animations or dynamic styling).
Maintainability: While performance is essential, maintaining readable and manageable code should also be a priority.
Browser Compatibility: Different browsers may optimize these operations differently, which might impact performance in real-world scenarios.
Other alternatives for styling could have included:
In conclusion, the choice of method for applying styles in JavaScript has significant implications for both performance and maintainability, and developers should choose based on the specific requirements of their applications while performing due diligence through testing.