HTML Preparation code:
AخA
 
1
<div id="test"><div style="height: calc(var(--w, 1) * 1px); background-color:red;"></div></div>
Script Preparation code:
 
el = document.getElementById("test");
rn = ()=>Math.floor(10+Math.random(5));
Tests:
  • style.setProperty

    x
     
    let i = 0;
    while (i < 10000) {
      el.style.setProperty("--w", rn);
      i++;
    }
  • style.cssText

     
    let i = 0;
    while (i < 10000) {
      el.style.cssText = `--w: ${rn()};`;
      i++;
    }
  • style

     
    let i = 0;
    while (i < 10000) {
      el.style = `--w: ${rn()};`;
      i++;
    }
  • Object.assign

     
    let i = 0;
    while (i < 10000) {
      Object.assign(el.style, {
      ['--w']: rn(),
    });
      i++;
    }
  • setAttribute

     
    let i = 0;
    while (i < 10000) {
      el.setAttribute('style',`--w: ${rn()};`);
      i++;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    style.setProperty
    style.cssText
    style
    Object.assign
    setAttribute

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 4 months ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
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