Skip to content

[cssom] Proposal: CSS.assign #4486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
WebReflection opened this issue Nov 6, 2019 · 4 comments
Closed

[cssom] Proposal: CSS.assign #4486

WebReflection opened this issue Nov 6, 2019 · 4 comments

Comments

@WebReflection
Copy link

WebReflection commented Nov 6, 2019

It would be lovely to have the ability to define multiple CSS properties at once, without causing reflows or repaint during each property that might trigger those.

example

CSS.assign(element.style, {
  width: w + 'px',
  height: w + 'px',
  top: y + 'px',
  left: x + 'px',
  transition: (rand(100) + 900) +'ms',
  background: `rgba(
    ${rand(255)},
    ${rand(255)},
    ${rand(255)},
    ${Math.random() + 0.5}
  )`
});

implementation

The CSS.assign(style, ...objects) would mimic 100% Object.assign but it would grant that all properties are set at once, making multiple re-style, or updates, faster than ever.

The polyfill for this proposal would look something like this:

if (!CSS.assign)
  CSS.assign = Object.assign;

Thanks for considering this proposal.

@WebReflection WebReflection changed the title [cssom [cssom] Proposal: CSS.assign Nov 6, 2019
@emilio
Copy link
Collaborator

emilio commented Nov 6, 2019

But setting multiple properties never causes multiple repaints, unless you read in between. So I think this is WONTFIX as in it works as you want already.

@Loirooriol
Copy link
Contributor

It would be lovely to have the ability to define multiple CSS properties at once, without causing reflows or repaint during each property that might trigger those.

Doesn't this happen already? AFAIK, unless you force it, styles are not applied synchronously.

For example, if I go to https://drafts.csswg.org/cssom/ and run

for (let i = 0; i < 100; ++i) {
  document.body.style.width = Math.random() * 1000 + "px";
  document.body.offsetLeft;
}

it takes like 8 seconds, because I'm forcing layout at each iteration. But if I remove the offsetLeft line, it's a single millisecond.

@emilio
Copy link
Collaborator

emilio commented Nov 6, 2019

Yes, @Loirooriol articulated better what I wanted to say :)

Closing unless there's something we're missing.

@emilio emilio closed this as completed Nov 6, 2019
@WebReflection
Copy link
Author

unless there's something we're missing.

I can't believe I've never measured this, but I remember (long time ago) using a single style.cssText = ... assignment 'cause it was way faster than assigning many properties one after the other.

I kinda thought the reason was because of mutations, in terms of resizing, but of course I was wrong.

Thanks a lot for the quick come back, and apologies for my ignorance in this regard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants