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 |
<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++;
}