Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
CSS: .setProperty vs. Object.assign vs. .style vs. cssText-append
(version: 1)
Tests whether manipulating an HTML element's inline styles is fastest via .setProperty(), Object.assign, .style or by appending to the cssText.
Comparing performance of:
setProperty vs Object.assign vs .style vs .cssText
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="benchmark"></div>
Script Preparation code:
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)}` }));
Tests:
setProperty
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++; }
Object.assign
let i = 0; while (i < numTests) { Object.assign(el.style, css[i]); i++; }
.style
let i = 0; while (i < numTests) { for (const s in css[i]) { el.style[s] = css[i][s]; } i++; }
.cssText
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++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
setProperty
Object.assign
.style
.cssText
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
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
Related benchmarks:
Set style vs setAtrribute("style") 2
Comparison of setting style via different methods
6 properties - style.setProperty vs style.cssText vs style vs Object.assign vs Object.entries with style.cssText vs setAttribute
【JiangNanGame】style setting test 2
setProperty vs .cssPropertyName
style vs. style.cssText - additional styles
1 css var - style.setProperty vs style.cssText vs style vs Object.assign vs setAttribute
6 properties - style.setProperty vs style.cssText vs style vs Object.assign vs setAttribute vs classList
CSS Variable swaps - style.setProperty vs style.cssText vs style vs Object.assign vs setAttribute vs classList
Comments
Confirm delete:
Do you really want to delete benchmark?