<div id="test"><div style="height: calc(var(--w, 1) * 1px); background-color:red;"></div></div>
el = document.getElementById("test");
rn = ()=>Math.floor(10+Math.random(5));
let i = 0;
while (i < 10000) {
el.style.setProperty("--w", rn);
i++;
}
let i = 0;
while (i < 10000) {
el.style.cssText = `--w: ${rn()};`;
i++;
}
let i = 0;
while (i < 10000) {
el.style = `--w: ${rn()};`;
i++;
}
let i = 0;
while (i < 10000) {
Object.assign(el.style, {
['--w']: rn(),
});
i++;
}
let i = 0;
while (i < 10000) {
el.setAttribute('style',`--w: ${rn()};`);
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 | 129.8 Ops/sec |
style.cssText | 170.0 Ops/sec |
style | 141.2 Ops/sec |
Object.assign | 417.9 Ops/sec |
setAttribute | 357.7 Ops/sec |
Let's break down the provided JSON and explain what is being tested in each benchmark case.
Benchmark Overview
The test compares four different ways to set CSS variables (also known as custom properties) on an HTML element using JavaScript:
style.setProperty()
style.cssText
(using template literals)style
(direct assignment)Object.assign()
with a style objectsetAttribute()
with the style attributeLibrary Used: None
None of the benchmark cases use any external libraries, so we won't be discussing any additional dependencies.
Special JS Features/Syntax: CSS Variables
The test focuses on the new CSS variable feature introduced in CSS3, which allows you to define custom properties and reuse them across different stylesheets. The --w
property is used as an example variable.
In JavaScript, these variables are accessed using the style
object or its methods like setProperty()
.
Benchmark Preparation Code
The provided code sets up a simple HTML structure with an element that will be used for testing. It also defines a script preparation function rn()
, which generates a random integer between 1 and 10 (inclusive).
Individual Test Cases
Each test case is defined in the JSON array, with each object containing:
Benchmark Definition
: The JavaScript code that sets up the benchmark.Test Name
: A descriptive name for the test case.Here's a brief explanation of each test case:
style.setProperty()
: Sets the --w
CSS variable using setProperty()
.style.cssText
: Sets the --w
CSS variable by assigning a template literal to cssText
.style
: Directly assigns the value of rn()
to the --w
property.Object.assign()
: Uses Object.assign()
to merge an object with the existing style object, setting the --w
property.setAttribute()
: Sets the style
attribute using setAttribute()
, passing a template literal that sets the --w
value.Benchmark Results
The provided benchmark results show the execution rate (Executions Per Second) for each test case on a Chrome Mobile 118 device with Android 10 operating system.
Here's a brief summary of the results:
Object.assign()
yields the highest execution rate, indicating that it is likely the fastest method.setAttribute()
comes in second, suggesting that using the style
attribute can be a viable alternative to other methods.style.cssText
and style.setProperty()
methods have slightly lower execution rates, possibly due to parsing or syntax-related overhead.Pros and Cons
Here's a brief summary of the pros and cons for each method:
Object.assign()
: Pros: Efficient, easy to use. Cons: May require additional setup, e.g., creating a style object.setAttribute()
: Pros: Simple, straightforward. Cons: May not be as efficient as other methods, e.g., due to attribute parsing overhead.style.cssText
and style.setProperty()
:Alternatives
Other alternatives for setting CSS variables include:
DOMTokenList
: An API that allows you to manipulate the style attribute of an element using a more modern and efficient approach.CSSStyleRule
objects: You can use the CSSStyleRule
object to create or modify a style rule, including setting custom properties.Note that these alternatives may have slightly different performance characteristics and usage patterns compared to the methods tested in this benchmark.