<div id="test"></div>
el = document.getElementById("test");
style = el.style
attributeStyleMap = el.attributeStyleMap
color = 'red'
border = '1vmin solid red'
padding = '.5vmin'
backgroundColor = 'black'
height = '1vh'
width = '1vw'
style.setProperty("color", color);
style.setProperty("border", border);
style.setProperty("padding", padding);
style.setProperty("background-color", backgroundColor);
style.setProperty("height", height);
style.setProperty("width", width);
style.cssText = `color:${color};border:${border};padding:${padding};background-color:${backgroundColor};height:${height};width:${width};`;
el.style = `color:${color};border:${border};padding:${padding};background-color:${backgroundColor};height:${height};width:${width};`;
attributeStyleMap.set("color",color);
attributeStyleMap.set("border",border);
attributeStyleMap.set("padding",padding);
attributeStyleMap.set("background-color",backgroundColor);
attributeStyleMap.set("height",height);
attributeStyleMap.set("width",width);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
style.setProperty | |
style.cssText | |
style | |
attributeStyleMap.set |
Test name | Executions per second |
---|---|
style.setProperty | 472450.4 Ops/sec |
style.cssText | 568467.8 Ops/sec |
style | 500901.4 Ops/sec |
attributeStyleMap.set | 192396.1 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark on the performance comparison of different approaches to setting CSS styles in HTML elements.
What is being tested?
Four test cases are compared:
style.setProperty
style.cssText
(using a string template)el.style
(assigning a string to the style property)attributeStyleMap.set
(using the attributeStyleMap
API)Options being compared
Each option is being tested for its execution speed, measured in executions per second.
Pros and Cons of each approach:
style.setProperty
, as it requires a fixed format for CSS declarations.style.setProperty
or attributeStyleMap.set
, but may incur performance costs due to the need to parse and execute the JavaScript code.Library and syntax used
The test cases use the following libraries and syntax:
attributeStyleMap
: A proprietary API provided by Chrome that allows setting CSS styles directly on an HTML element.el.attributeStyleMap
: Accessing the attributeStyleMap
property of an HTML element.No special JavaScript features or syntax are being tested in this benchmark.
Other alternatives
If you're looking for alternative approaches to setting CSS styles, consider:
style.cssText
or attributeStyleMap
.Keep in mind that the performance differences between these approaches may vary depending on your specific use case, browser, and implementation details.