question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent multiple style recalculations and improve performance by grouping DOM reads and writes

See original GitHub issue

Is your feature request related to a problem? Please describe. When requesting offsetLeft (or other DOM reads) in the beforeUpdate callback the browser has to recalculate styles when the styles are changed in a further update. See below how this looks in dev tools with multiple components (the second one is without the DOM reads, you can see the recalculation is done after every style has been updated).

I’ve created a REPL: https://svelte.dev/repl/1977c00357cc496eae5ec1f2a9d23675?version=3.12.1

Multiple recalculations because of DOM reads

Screenshot 2019-10-22 at 11 29 48

Single recalculation

Screenshot 2019-10-22 at 11 30 38

This slows down the frame render.

Describe the solution you’d like If we can schedule all reads before the update happens DOM reads are free (the browser has prepared the frame) and don’t cause additional style recalculations. This might require setting up a global draw loop so web components created with Svelte all use the same read/write order.

Describe alternatives you’ve considered I’ve moved the DOM read to afterUpdate while that does group part of the writes we only get the info after the update while we need it before.

Screenshot 2019-10-22 at 11 39 18

How important is this feature to you? Very. It’s super useful to be able to access layout information at the start of the frame (it’s free at that moment) and this can be used to for example calculate the width of a component and then layout its children accordingly (simulating component queries) without affecting render performance. I use this extensively in FilePond.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
frederikhorscommented, Jun 26, 2021

Oh my gosh this stale!

1reaction
frederikhorscommented, Dec 23, 2021

Nope

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reduce the scope and complexity of style calculations - web.dev
Reduce the scope and complexity of style calculations · Reduce the complexity of your selectors # · Reduce the number of elements being...
Read more >
DOM performance case study - Arek Nawo
First, the best and most popular technique is known as batching. What it basically means is that you should group your DOM read...
Read more >
Browser Rendering Optimization - James Priest CV
The next part of the process is to combine the DOM and CSS. bro1-10. In the tools you're going to see this as...
Read more >
10 Ways to Minimize Reflows and Improve Performance
Make class changes on elements as low in the DOM tree as possible (i.e. elements that don't have multiple deeply nested children). This...
Read more >
Reflows & Repaints: CSS Performance making ... - Stubbornella
Avoid setting multiple inline styles ... We all know interacting with the DOM is slow. We try to group changes in an invisible...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found