Transcript from the "Layouts and Reflows" Lesson
[00:00:54] Because, yeah, if you do it too much, that's problematic. Again think about all of the elements on the page, every list item, every text node, everything, every image. It's a lot of work. I don't even know how it gets it done in the time that it does. It's kind of impressive, like, I think it's mostly that we've had like 29 years or now I think the web is, 29 years of experience doing this, has made it decently fast.
[00:01:18] But like it's incredible. You can actually, there are some YouTube videos that show Wikipedia, or a few other pages effectively, the process of Firefox goes through laying it out. It's like okay, there's an H1 it's inside of a div. And it keeps having to move it around and change the size and adjust stuff and it's fascinating.
[00:01:35] So kind of mentioned this before. Whenever the geometry of an element changes the browser has to reflow the page, right? Like, how that actually happens like in the browsers is very different, right. Like it doesn't really matter. The major points we're going to touch on is not doing it when you don't need to, it's the largest one, right?
[00:02:11] That is the thing that we are doing right now. Stop everything until that is done. And obviously if I haven't made this clear enough, it's hard. And it takes a lot of resources and it's not fast or easy. And if you do it a lot, guess who's gonna notice, right.
[00:02:26] A lot of the stuff we're trying to do is, for a lot of this web performance stuff, if we're doing our jobs well, the user should never notice that we're doing anything. Users never go, they implicitly in their brains go like, I like this side. They don't know that performance is the reason why.
[00:02:43] But, if performance on your side is terrible, they know, [LAUGH] right? If users notice the performance, it's usually that you're messing it up, right? So you end up doing this in a loop. It's gonna be noticeable, it's gonna be bad. And we basically don't want people to notice us.
>> Steve: Coo,l the only thing to keep in mind is that reflow of an element. I'm very careful with these words, right. Reflow of an element causes the reflow of its parents and children. Think about what you know about a tree. [LAUGH] If you reflow all of the children and then you reflow the parents and they reflow the children.
[00:03:22] Pure most the time, there are some edge cases where you can get away with only having to reflow part of it and we'll talk a little about some of those. But generally speaking that is code for saying you touch one thing you touch everything, right, this is technically the truth.
[00:03:36] But really more often I would say 95% of the time you reflow a thing, you're reflowing everything. Good luck. So, okay, we know that reflows are expensive. Avoiding them is not necessarily fair cuz sometimes you do need to change the layer of the page, right? So, it's as much as you need and as little as can get away with kind of thing.
[00:03:57] So what causes a reflow? Here is a incomplete list and yes, the small font is a joke, [LAUGH] right? It's a way to put as much on this slide as possible, right? Resizing the window, changing the font, changing the content, adding or removing a stylesheet, adding or removing classes, adding or removing elements.
[00:04:14] Changing the orientation, like when you take the phone from portrait to landscape, obviously, like that reflows the page. Calculating size or position. That one's interesting. The simple act of trying to figure out the size or position of something, causes a reflow. Fascinating. Changing the size or position, like yeah, of course that causes a reflow.
[00:04:34] And when you reflow and you change the size of a bunch of stuff, guess what you got to do next? Right, you got to repaint, because you now need to paint more pixels blue if it's a blue element that you changed the size of. More or less, I'll give you a hint.
[00:04:52] Layout is the second most expensive part of the rendering pipeline. Would you like to take a guess at what the first one is? Painting,and for every time you reflow every time you do a layout you have to do a paint, guess what, they always come usually as a package deal.
[00:05:07] So questions how do we avoid this? Yeah,you can try like if can't get away with it changing like the lowest levels of the DOM trees and not get that whole page reflow, like give you change the container element from 600 pixel wide to 800 pixels wide, everything inside of it is gonna change.
[00:06:05] I want to animate every single pixel, right? If that takes longer than your 16 milliseconds and you're going to miss that frame, right? Because you're triggering all this reflow. You're better off moving that box three pixels at a time, right? Because you wouldn't have actually missed as many frames.
[00:06:22] So sometimes, writing the animation to be a little bit jankier makes it faster because you don't miss the next frame that's gonna be painted. The other one is avoiding table layouts, that kinds of makes sense for a lot of reasons, one like I write similar emails, just trust me.
[00:06:38] [LAUGH] All right, a table here is hard, but the other part is like, if a column and a table gets wider, what do you think happens like every other cell above it, and below it? It's got to give, you know reflowed. A table effects, everything around it by definition.
[00:06:53] And then batting down manipulation, this one, if you're using a framework, you effectively getting it for free, right. This is what makes React, React, right? It's what makes Ember, Ember, and Angular, Angular, is that they figure out how to take the state of your application. And do the bare minimum of changing the page, and doing it as efficiently as possible, right?
[00:07:13] So, rather than, I'm gonna add all these. Like, you'll change a bunch of stuff in your application. It will figure out a pro forma way to do that. That is like legitimately what a lot of our frameworks are bringing to the table, right? Is the, we will let you make a bunch of changes to the state of your application, we will figure out an efficient way of rendering it.
[00:07:47] And that they make a lot of assumptions that have to be general. A given framework has to be as good for you as it is for the person sitting next to you. Which means that they're are kind of like Jack of all trades, they're making best case assumptions.
[00:07:57] If you were going to fine tune stuff. Yeah, like you could probably make it faster than if you used react but you'd have to do that for everything and a lot of times we make the trade off of a framework making good decisions for us most of the time right?
[00:08:11] So that like we make them more often right? And then debouncing recess events, which is, if you are, have an eventless scenario that's listening for the window to resize. Typically for me, this happens if I'm making a data visualization with an SVG and I want to change the width of everything, like when the window resizes.
[00:08:31] They usually dragging it. So usually you dont want to do it repeatedly. You say, okay, I have to drag it and let me do this every so often, you'll have less reflows.