Run details:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Mobile Safari/537.36
Chrome Mobile 118
Android
Mobile
one year ago
Test name Executions per second
style.setProperty 19.6 Ops/sec
style.cssText 21.8 Ops/sec
style 17.8 Ops/sec
Object.assign 109.0 Ops/sec
setAttribute 88.8 Ops/sec
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++;
    }