<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;
const obj = {
color: 'red',
border: '1vmin solid red',
padding: '0.5vmin',
'background-color': 'black',
height: '1vh',
width: '1vw'
}
const forgeStyleString = () => {
return Object.keys(obj).reduce((p, key) => {
return p + `${key}: ${obj[key]}; `
}, '')
}
while (i < 10000) {
el.setAttribute('style',forgeStyleString());
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 | 107.1 Ops/sec |
style.cssText | 114.7 Ops/sec |
style | 104.0 Ops/sec |
Object.assign | 108.1 Ops/sec |
setAttribute | 365.8 Ops/sec |
The benchmark outlined in the provided JSON focuses on evaluating the performance of different methods for applying CSS styles to a DOM element in JavaScript. The tests specifically compare five different approaches for styling, and the goal is to determine which method performs best under repetitive use.
style.setProperty
style.setProperty
setProperty
method of the CSSStyleDeclaration
interface to set individual style properties in a loop.style.cssText
style.cssText
style
Assignment
style
cssText
, it allows bulk updates of styles.cssText
, it can lead to overwriting existing styles.Object.assign
Object.assign
Object.assign
to copy those styles onto the element’s style property.setAttribute
with Style String Generation
setAttribute
setAttribute
.setAttribute
could be slower due to string manipulation and function calls.style.cssText
.Based on the benchmark results provided:
setAttribute
approach, indicating that constructing a style string and setting it in one go is the most efficient for large scale updates.style.setProperty
was among the slower options, as it individually sets each CSS property, accumulating overhead.While the above five methods are valid and commonly used in JavaScript, other alternatives include:
CSS Classes: Instead of manipulating inline styles, toggle CSS classes on your elements using classList.add()
, classList.remove()
, or classList.toggle()
. This can offer considerable performance benefits and better maintainability, especially for complex styles defined in stylesheets.
CSS-in-JS Libraries: Libraries such as Styled Components and Emotion allow for a more dynamic approach to styling in React applications. These have their own trade-offs in terms of performance, especially with server-side rendering considerations.
Understanding these different approaches and their implications on performance can guide software engineers in optimizing their applications' styling strategies effectively.