<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 replace | |
Object.assign | |
setAttribute | |
style update |
Test name | Executions per second |
---|---|
style.setProperty | 7.9 Ops/sec |
style.cssText | 16.9 Ops/sec |
style replace | 15.5 Ops/sec |
Object.assign | 9.9 Ops/sec |
setAttribute | 330.4 Ops/sec |
style update | 8.7 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark, named "7 properties - style.setProperty vs style.cssText vs style vs Object.assign vs setAttribute," compares the performance of different methods for setting CSS styles on an HTML element. The goal is to identify which method is the fastest and most efficient.
Test Cases
There are five test cases:
setProperty
function, followed by a loop that applies multiple properties with varying units (e.g., %
, em
, etc.) to the element.cssText
property of the element's style attribute. The test case is similar to the first one, but uses this approach instead.el.style.color
).assign
function and applying it to the element's style attribute.style
attribute of the element, followed by a loop that applies multiple properties to the element.Libraries and Special Features
None of the test cases use any external libraries or special features like ES modules, async/await, or Web Workers.
Performance Comparison
The benchmark results show that:
Alternatives
If you want to explore other approaches for setting CSS styles on an HTML element, consider these alternatives:
css
API: The Web APIs provide a css
interface for styling elements. You can use this API to update styles efficiently.Keep in mind that these alternatives may have different performance characteristics or require additional setup and syntax changes.
In summary, the benchmark results suggest that setAttribute is a good choice when you need to set individual property values quickly, while style.setProperty and style.cssText might be better suited for situations where multiple properties need to be applied with varying units.