<div id="benchmark"></div>
const numTests = 10000;
const el = document.getElementById("benchmark");
const css = (new Array(numTests)).fill(0).map(() => ({
"opacity": `${Math.random()}`,
"zIndex": `${Math.floor(Math.random() * 1000)}`
}));
let i = 0;
while (i < numTests) {
for (const s in css[i]) {
el.style.setProperty(s.replace(/([a-z][A-Z])/g, (m) => m[0] + "-" + m[1].toLowerCase()), css[i][s]);
}
i++;
}
let i = 0;
while (i < numTests) {
Object.assign(el.style, css[i]);
i++;
}
let i = 0;
while (i < numTests) {
for (const s in css[i]) {
el.style[s] = css[i][s];
}
i++;
}
let i = 0;
while (i < numTests) {
console.log(i, css[i]);
const s = Object.entries(css[i]).map(([key, value]) => `${key}:${value}`).join(";");
el.style.cssText += s;
i++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
setProperty | |
Object.assign | |
.style | |
.cssText |
Test name | Executions per second |
---|---|
setProperty | 32.5 Ops/sec |
Object.assign | 35.3 Ops/sec |
.style | 35.9 Ops/sec |
.cssText | 31.9 Ops/sec |
The benchmark on MeasureThat.net is focused on comparing the performance of different methods for manipulating the inline styles of HTML elements in JavaScript. The methods evaluated include:
element.style.setProperty()
Object.assign()
element.style
element.style.cssText
setProperty
: This method allows setting a specific CSS property on an element. It takes two parameters: the property name (formatted traditionally as a CSS property with dashes) and the value. The value is applied exactly as intended, which can involve converting camelCase property names to dash-separated names (e.g., backgroundColor
to background-color
).
Pros:
Cons:
Object.assign
: This method is used to copy the properties from one or more source objects to a target object. Here, it copies CSS properties from a predefined object to the element’s style object.
Pros:
Cons:
Direct assignment via .style
: This method involves directly manipulating the style
object of the element by setting individual properties.
Pros:
Cons:
Appending to cssText
: This method modifies the cssText
property of the style
object, which is a string representation of all inline styles. By adding new properties as a string, it concatenates styles directly.
Pros:
Cons:
The benchmark does not utilize any external libraries, relying solely on the built-in DOM and JavaScript functionalities. All the methods are part of the standard JavaScript API for manipulating the Document Object Model (DOM).
The benchmark results show the executions per second for each method in the Safari browser on a specific desktop environment. The el.style
(direct assignment) method yielded the highest performance at approximately 35.93 executions per second, followed by Object.assign
and setProperty
. The method that performed the least well was appending to cssText
.
In addition to the methods measured, there are various other approaches to style manipulation in JavaScript:
Using CSS Classes: Instead of directly manipulating styles with JavaScript, toggling classes on elements can be more efficient. This approach separates concerns and allows for cleaner and more maintainable code.
CSS-in-JS Libraries: Libraries like styled-components or Emotion can dynamically generate styles and encapsulate CSS rules within JavaScript, making style management more organized and consistent.
Framework-specific Solutions: For applications using frameworks like React, Angular, or Vue, the respective paradigms (like JSX styling in React) provide optimized methods to handle styles within components efficiently.
Ultimately, the choice of style manipulation method depends on the specific use case, performance needs, and maintainability considerations within the project's context.