TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Preventing 'layout thrashing'

175 pointsby makepanicover 11 years ago

14 comments

icambronover 11 years ago
Can someone explain why the &quot;Quick Fix&quot; even works? It seems like if having the DOM updates separated by one statement triggers two reflows, so would two consecutive updates. Or more generally, what does &quot;batching DOM updates&quot; really mean?<p>Does the browser just pay attention to whether each line of JS updates the DOM and queue up its updates until it encounters one that doesn&#x27;t? Doesn&#x27;t fit my model for how the JS engine fits into the browser. I guess I don&#x27;t really know, but I always assumed it just reflowed on a fixed timeout.<p>Edit: Nevermind, I get it: it&#x27;s that the intervening statement <i>reads</i> from the DOM, thus triggering a flush. I just missed that in the article.
评论 #6501499 未加载
andrewaylettover 11 years ago
This seems to be the equivalent of Flex&#x27;s &#x27;callLater()&#x27;, which was the bane of my life back when I did Flex, as it almost completely decouples the called code from the calling code -- very difficult to work out what called the code if it&#x27;s failing and very difficult (without good comments) to know <i>why</i> it was added.<p>We had a rule: If you think you need a callLater(), you don&#x27;t need to use callLater(). If you still need a callLater(), you need to get someone to come and look at your code <i>now</i> to tell you that you don&#x27;t need to use callLater(). If you both agree that you need to use a callLater(), you&#x27;ve still got to justify it at code review time.<p>The biggest difference I can see at the moment is that Flex doesn&#x27;t recompute layout until the end of the frame, even if you do read from it. JS does recompute, so you need to defer for performance rather than (as in Flex) correctness. In either environment, the sane thing to do is to avoid having to defer your calls at all. It may be more work now, but your sanity will thank you later.<p>As an example of how bad things can get, Adobe&#x27;s charting components would take more than 13 frames to settle rendering, because of all the deferred processing. This is a good example of how deferring your calls can actually cost you quite a lot of performance.
patmcguireover 11 years ago
If you have the default sidebar on Ubuntu 12.04 and a 2560-pixel wide screen, and give Chrome exactly half of the width (I have a grid plugin), some Wikipedia pages will resize themselves about 15 times a second as they realize that they should change their layout, but then the change means that they should shrink down to the previous version, and then that change... can&#x27;t find one that triggers that or I&#x27;d link to a video.
评论 #6502734 未加载
评论 #6502385 未加载
solox3over 11 years ago
Well-researched feature. The best part of the fastdom wrapper (<a href="https://github.com/wilsonpage/fastdom" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;wilsonpage&#x2F;fastdom</a>) is that a timeout stub is introduced even for browsers that don&#x27;t support native animation frames. Good job.
评论 #6501373 未加载
评论 #6501745 未加载
OliverMover 11 years ago
This is something of a solved problem for many of the major javascript frameworks. Sproutcore (just to pick an older example I&#x27;m familiar with) has had this licked since 2008; you put all your DOM-upating code in your view update calls, and Sproutcore pipelines the calls. I&#x27;m sure most of the other JS MVC frameworks have similar solutions.
arayover 11 years ago
This seems very similar to the way mobile devices use vsync&#x2F;vblank to organize work into frames. [0] Very cool!<p>[0] Good explanation of how this process works on android: <a href="http://www.youtube.com/watch?v=Q8m9sHdyXnE" rel="nofollow">http:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Q8m9sHdyXnE</a>
评论 #6501759 未加载
stu_kover 11 years ago
We use a technique similar to this in Montage, which we call the draw cycle: <a href="http://montagejs.org/docs/draw-cycle.html" rel="nofollow">http:&#x2F;&#x2F;montagejs.org&#x2F;docs&#x2F;draw-cycle.html</a>. Because it&#x27;s built into the components, <i>everything</i> in the webapp reads from the DOM at the same time, and then writes to the DOM at the same time, completely avoiding the thrashing.
nsxwolfover 11 years ago
There doesn&#x27;t seem to be a definition of layout thrashing anywhere on the internet. Googling &quot;what is layout thrashing&quot; returns nothing.<p>Anyone want to offer the net&#x27;s very first ever explicit definition of this term?
评论 #6501669 未加载
评论 #6502528 未加载
评论 #6501713 未加载
评论 #6502347 未加载
bookfaceover 11 years ago
Wouldn&#x27;t another solution to this be to have an object that would mimic the dom, performing reads immediately (or reading from its own cache of written attributes), but allowing explicit control over when writes get committed? It would then be easy to have atomic (wrt dom layout) functions.
ehsanu1over 11 years ago
This seems like something that should be solved by the dom api, rather than this (quite clever) workaround.
评论 #6501465 未加载
评论 #6501376 未加载
emehrkayover 11 years ago
The Sencha touch guys passed everything, event events, through a requestAnimationFrame call as an object pool for their Facebook demo and it seemed to scale well. I wonder why more people do not explore that approach
评论 #6505083 未加载
stingraycharlesover 11 years ago
Doesn&#x27;t this open up the possibility of race conditions?
评论 #6501388 未加载
mrtksnover 11 years ago
Hmm, can this be implemented in JS frameworks, so that can be done with less code?
评论 #6501737 未加载
fvox13over 11 years ago
Maybe if people stopped using Javascript for things it shouldn&#x27;t be used for, we wouldn&#x27;t have this problem...
评论 #6501628 未加载
评论 #6501693 未加载
评论 #6501722 未加载
评论 #6501717 未加载
评论 #6501912 未加载