chantastic.fm

37: Decide with Your Human Brain, with Brian Vaughn. On the new React profiler, windowing, and intelligent performance tuning.

Episode 37 · February 28, 2019 · 00:41:12

Hosted by chantastic · With Brian Vaughn

Sponsored by Linode

Download MP3 · Jump to transcript

Brian Vaughn is a member of the React Core team and creator of libraries like react-virtualized and react-window. He's a wealth of knowledge in React performance and application profiling.

Chantastic asks Brian about the new profiler tools he's been working on (available to React v16.5 apps), React Core team dynamics, and the future of windowing in React and browsers.

They discuss a handful of practical performance tips, Concurrent rendering in React, React.memo and the useMemo Hook, and how to decide with your human brain when performance tuning is necessary.

Featuring

Links

Sponsors

Linode

Instantly deploy and manage an SSD server in the Linode Cloud, with your choice of Linux distro, resources and location. linode.com/react

Transcript

[00:00:00] Michael Chan: This is React Podcast. I'm Chantastic. Today we have the incredible privilege of sitting down with Brian Vaughn, core team member of React, and talking about the work that he's been doing in the React Profiler, available to apps using React 16.5 or later. He gave an incredible talk at React Conf, and if you haven't watched it, you should just pause this and and watch it on all of the new cool tools for profiling a React app. We talk about other open source work of his that you might be familiar with, React Virtualized and React Window, and how some of those problems will start to be solved in React in the future. Consequently, we talk about the state of windowing both in React and in the browser. He gives us some really interesting insights into the makeup and dynamics of the React team and strategies for intelligently improving the performance of our apps today. I think you'll really enjoy this chat. We were super lucky to be able to sit down with Brian Vaughn. If anything catches your ear and you'd like to learn more, visit reactpodcast.com/37. Thanks this week to our sponsor, Linode. Instantly deploy and manage an SSD server in the Linode Cloud. Get a server running in seconds with your choice of Linux distro, resources, and node location. Now offering dedicated CPU instances designed for consistent high-performance computing needs like video encoding, game servers, and busy applications. This is Linode's first time sponsoring the show, and I want to tell you a little story about why I love Linode so much. During the period of my life when I was transitioning careers and I was on unemployment, I was having to deploy my first application to the internet for a client that I had figured out somehow to magically pay me. Up to that point, I had done absolutely nothing on the backend. Linode made it so incredibly easy. It's because of their support that I was able to fake it till I make it. If you'd like to try them out, you can get a $20 credit when you use the promo code REACT2019. Just visit linode.com/react. Brian, thanks for sitting down with me. This is it's super fun to to have a moment to get away from the conference and sit with you for a second.

[00:02:28] Brian Vaughn: Yeah, no problem. Happy to do it.

[00:02:30] Michael Chan: Uh, so we're recording this at React Conf 2018. I keep messing that year up. Um, we're here, and this morning you gave a a talk. You and Andrew gave a talk. Um. Talking about the new features in Suspense and also the new profiler that you built for for React. Uh could you tell me a little bit about the high level of kind of what that talk was about?

[00:02:52] Brian Vaughn: Yeah, uh so I guess Andrew's talk was just showing how you can use the new Suspense APIs and also Concurrent sort of to incrementally make um your app better, feel more responsive, less janky, um sort of also a theme of like simplifying your code and the developer experience. And then my talk about the profiler kind of added on to that. Um, I think we realized early on that async uh rendering, async programming in general is just trickier.

[00:03:23] Michael Chan: Yeah.

[00:03:24] Brian Vaughn: To reason about. Browser tools have done a lot of great things like stitching together async call stacks and stuff, but it's still just harder to reason about, especially if you're thinking of performance and you're looking in the performance tooling and you see a bunch of time-sliced call stacks and you have to stitch them together with your brain and make sense of them. And uh we we want to sort of have React be something that's usable by all audiences. Sure. And we want the developer experience to be nice. And so invested a lot in the last few months, particularly uh something I worked a lot on, on just adding performance tooling and profiling capabilities. And so my talk was sort of an introduction to those tooling for people who maybe weren't aware before. Definitely a high level, like, here's just how it works, and then some links to like learn more. But with the theme of how to take this like complicated rendering model and like distill it down to something that you can look at and like easily digest what's going on.

[00:04:19] Michael Chan: Yeah, yeah. How long have you been working on the profiling tools for Concurrent Mode?

[00:04:24] Brian Vaughn: A couple months.

[00:04:25] Michael Chan: A couple months?

[00:04:26] Brian Vaughn: Yeah.

[00:04:26] Michael Chan: Yeah. What was the hardest problem to solve solve in stitching that data back together in a human-readable way?

[00:04:34] Brian Vaughn: Um, I think there are probably two, uh, maybe two things. Um, one of them is just React 16's uh architecture called Fiber is has some inherent complexity that, especially with async rendering, uh can be hard sometimes to wrap your brain around. Um, so like in part it was challenging to learn parts of the code that I'm I wasn't super familiar with. I mean, the React team is small, and so we don't- and the React projects and the renderers are big, so like I don't think we don't even have- I mean, there are parts of ReactDOM, especially the event system and stuff, that no one on the current team has great familiarity with.

[00:05:12] Michael Chan: Oh wow.

[00:05:12] Brian Vaughn: But also there are parts where only one person is intimately familiar with, and so like learning- and I had to lean on, you know, folks like Andrew and Sebastian who were more familiar with it, definitely, to figure out the right ways to do The profiling bit. In particular, the labeled state updates bit, the tracing API, I remember was it sparked from Sebastian and Andrew and I went out and sat on the- Facebook has a little outdoor patio area in our office, and we went out and had a coffee there a couple of months ago. And I think Andrew just mentioned like, yeah, we could probably even use expiration times to track state updates. And I was like, oh, that's really exciting. And then it was a lot of like trial and error figuring out how to to model that correctly. But yeah, uh I think you asked how long I've been working on it and what was the other the hardest thing. Oh yeah. Yeah, yeah, yeah. So another thing that was hard but really fun for me was um the challenge of building uh an interface that was very performance sensitive. Yeah. Um like I mean I'd worked on virtualization and windowing stuff before, so I was familiar with that aspect, but the flame graph that Profiler shows, um can be huge, uh especially for some of Facebook's larger apps like Ads Manager. It can have many, many thousands of nodes and it potentially gets these snapshots like many times a second. Um and so it's a lot of data and you don't wanna- the apps that have more going on need profiling the most, so you don't want profiling to just crash on those apps because it's too expensive. Right. And so it's a lot of time spent sort of figuring out how to collect the minimum amount of information I needed while profiling and then just-in-time lazily uh parse it and then like what do I cache versus throw away to keep the memory footprint under control so that I don't crash the the plugin after a while. And that was a lot of trial and error um and uh just trying large apps and seeing like oh it doesn't even work on this one, like how can I make that work? Uh and that was a lot of fun and I think led to some improvements in uh React Window, my one of my libraries that I use in the profiler. So that was fun.

[00:07:11] Michael Chan: Nice, nice. So when you say that the— so the the React team has obviously changed uh quite a bit over the last um 5 years. Years that it's been like a public product. Have those- have the people who kind of initially created it, are they still like involved at Facebook or have they they left? And how how has that team transition kind of been over the few years?

[00:07:34] Brian Vaughn: Yeah, some of them are still at Facebook and just working on uh different but related things. And then some longtime contributors like uh Sophie and Sebastian are still on the team. But uh Dan has been on the team for maybe 3 years, Andrew and I for about 2 years, Dominic for a little less. So, uh, I mean, 2 years is a long time, but it's it isn't, it isn't, you know, like 2 years I've been also learning like React Native and Facebook infra, and it's a lot of stuff. Um, and you also sort of learn on demand, and so there are parts that we just, I just haven't had a need to go into, and I think that's true for everyone, you know, as we work on some of these new features, there's a lot of time just spent doing exploratory stuff.

[00:08:18] Michael Chan: Yeah.

[00:08:19] Brian Vaughn: Coming up with the ideas for the APIs and then testing them and finding out what doesn't work and what does work. And so we kind of, I think, have some silo, like, specialization going on there. Like, I pretty much exclusively worked on the profiler. Some other people did some code reviews, but that was it. And then I think Andrew has uh more or less done like most of the Suspense stuff and we we sort of have our silos like that. So that's something that we have a goal to reduce, like do more pair programming and do more you know shared ownership of stuff. But then when it comes down to it and you've got this like goal to finish everything by a certain date, then you sort of naturally organize to like the most efficient set of stuff. So.

[00:09:02] Michael Chan: Divide and conquer.

[00:09:03] Brian Vaughn: Yeah. Yeah, uh, so yeah.

[00:09:05] Michael Chan: What's the uh what's the team dynamic of the React Core team on a day-to-day basis?

[00:09:11] Brian Vaughn: Can you elaborate a little bit?

[00:09:12] Michael Chan: Yeah, sure. Um, so it's like day-to-day, do you feel like there's more— um, working on React Core, is it is the path like pretty well laid out or is there a lot more like high-level discussion that you need to have and planning and road mapping or Does it feel kind of responsive at times? Like we know that this needs to it needs to be in place or be done to support our vision, so we're just kind of chugging along and getting that out.

[00:09:40] Brian Vaughn: It's cyclic. I think at a high level, Sebastian in particular has had a lot of ideas for things that we're building out in his head that go back a couple of years.

[00:09:51] Michael Chan: Yeah.

[00:09:52] Brian Vaughn: Um, so he's had sort of a long-term vision. And then, you know, like, a lot of the things that we're just now releasing have also been in the works for 2 years. And so there's also been long-term efforts working on them. Um, but then we've also done a lot of releases in the meanwhile and features that have gone out that are, and sometimes in support of, but also sort of a little tangential. And I think those things are kind of self-organizing. Um, so for me personally, for instance, I, when I first joined the team, In part because there was a need, but also in part because I was intimidated to dive into certain parts of core. Um, I worked on a lot of ancillary stuff, so I worked on the the website, I worked on a bunch of our release scripts and the dev tools and stuff that was like support stuff. And I think like I had the freedom to do that. Um, our manager Sophie is very supportive. We all have a lot of autonomy. Uh, she makes course corrections if we need it. I remember at one point she said, This was around the release of 16.0, and I'd been working on the website for a while, and she's like, you probably should be wrapping that up if you can, because like we want you to be working on these other things too. But I that was the only time I can recall ever having a conversation like that with her in 2 years. So really it's like we we as a team understand there's a big big bucket of things we could work on. Um there's only a few of us, and so there's some uh flexibility and autonomy to sort of pick what you're passionate about. Then other times, you know, we all sort of rally around the goal. So when we decided to try to have this conference align with like the announcement of Hooks and the release of this alpha and stuff, um we realized that was a stretch goal. And so we, um in this case, we we would have like weekly meetings where we'd all get together and say, all right, here's, we made a ClickDoc, we started just to list everything we had to get done and we picked like semi-arbitrary dates, but the rough sequencing was correct to get us there. And then we You would just add our names and check boxes off. And so in that case, we were working more like a lightweight version of product teams I've been on in the past where you have like a roadmap, but that's rare. I don't think we normally have deadlines or deliverable dates because a lot of what we do is, especially lately, it's like exploratory.

[00:12:04] Michael Chan: Yeah.

[00:12:04] Brian Vaughn: It's in support of some some goals that Facebook and the larger open source community, like things that will help them, but It's like it's hard to put a time on like design an API, try it out, respond to feedback. That's sort of like uh you just iterate until you reach this point of stability and it's hard to plan that. So it's a mix.

[00:12:22] Michael Chan: Yeah. Now one thing that I've seen and I really love about seeing you interact as a core team at conferences like this is that you give each other a lot of space and you uh listen to each other, you listen to other people. Is that something that is is practiced and is a goal, or is that just kind of the nature of the people who make up that team?

[00:12:46] Brian Vaughn: It's probably both. I think uh everyone on the team- well, like I said, Sebastian and Sophie have been on the team for a long time, but those of us newer to the team um have been hired from- we've been recruited onto the team from a background that consists of a lot of open source work.

[00:13:04] Michael Chan: Okay.

[00:13:05] Brian Vaughn: You know, Dan and Andrew had Redux, um and some other projects there. I had virtualized and some Redux plugins. So like we we come from, I think, like, well, a background of showing that we're like passionate about open source, but also um having had the experience of a couple of years of like interacting with people and taking feature requests and like prioritizing and communicating and stuff. And I think a big part of that is empathy because Without it, you'll burn out or you just won't do it in the first place. And so I guess like, yeah, I mean, I think there's something about uh a personality type that's attracted to open source that's maybe necessarily a little more open to like that sort of conversation loop. Um, but it's also definitely um an active goal too. You know, I think again, the people who have been on the project for longer, and including people like Tom who's like higher up in the management chain though, but they've work to have a certain like vibe in the community- that's not the right word- but basically like a sense of like welcome and openness. Yeah. And we want to be uh beginner to advanced friendly. And and uh and I think that's like picked up and sort of caught on a life of its own. And so it's easier to join this project now that that's the established tone and just keep it up than it would be to like start it from from fresh, you know. Like we have the React team kind of almost has like a voice and a demeanor. And so, and that was there, that predates most of us on the team now.

[00:14:35] Michael Chan: Yeah, yeah, yeah. I remember that was one of the things that attracted me to React initially. Because I was I was investigating, I I had done a little bit of Angular and I was investigating Ember because there were some things that I really liked in it. And when React came out, I really liked the non-combative nature of the people communicating about it. It's like, this doesn't have to be better than the other things to be good. It just works for us and we like it, and that's awesome.

[00:15:06] Brian Vaughn: Yeah, yeah, I think that's that's been that's been my experience too. The community's been like really friendly and really welcoming and pretty uh not not a lot of big egos or anything, and it's been it's been good. Probably the most like positive uh of the just like engineering communities I've been in.

[00:15:26] Michael Chan: Yeah, I would agree. Um, so what brought you what brought you to React?

[00:15:31] Brian Vaughn: The team or just the framework in the first place?

[00:15:33] Michael Chan: I guess both, yeah, yeah, yeah.

[00:15:35] Brian Vaughn: Yeah, I joined uh a company um that was— uh I joined a company called Treasure Data, who um it's a data company. They had a web console for like managing the big data. Um and when I joined, they were just undergoing a rewrite from Angular to React. That sort of was decided by some engineers who were already on the team, um who were sort of like passionate about trying this new thing. Um and I joined, uh and I was like, okay, I haven't really used it, uh I'm I'm game. And I really liked it. And um also- You know, the working on this big data console that we hit some, probably obvious in hindsight, but like there were fun, novel challenges at the time just in terms of scale.

[00:16:20] Michael Chan: Yeah.

[00:16:21] Brian Vaughn: And there were two uh particular needs that we had for our admin interface that we were building that would manage, it was uh kind of a typical CRUD interface, but it managed big data. So you had, you know, a page that managed databases and tables and records and queries and a bunch of interfaces that were comparable and we had filtering and and and infinite scrolling was the the UI we wanted to build, but we had a lot of performance problems with this. Sure. So that, um, I guess, kind of my just inherent, like I I like the idea of building and sharing things open source, so I that um those business needs was what led me to go out and create React Virtualized and Redux Search. Um and we used both of those in the application we were building. And I open-sourced them too. And then through that I met, I guess, Dan initially. He tweeted about them and even at the time he had a huge following and so that drove a lot of attention to the libraries, which is cool. And then through that eventually I also met Sophie and the the team and then sort of, yeah, I just happened to work out that a year or so down the road they were looking to bring someone in to build windowing into React, which never happened but will be happening soon. I'm gonna start working on that in a month or two actually. Is the plan, but it's been the plan before. But yeah, it was very serendipitous and it just worked out that way.

[00:17:37] Michael Chan: That's that's awesome. Um so you've mentioned uh React Virtualized. What is the job that React Virtualized does for you?

[00:17:47] Brian Vaughn: Yeah, so there was a Facebook-led um package called FixedDataTable, which is what Treasure Data was using initially. And FixedDataTable um is a UI component. Uh I don't think it's actively supported for a while now. But it's a UI component that renders tabular data. Mm-hmm. And basically it's meant to render a lot of data, but in a way that's performant. There were there were a couple of challenges that use cases that we had that fixed data table didn't support, and I just decided to build us a lighter weight version of of of a similar library, which was what led to Virtualize. But basically the idea behind these things is that In some cases, you you have a lot of content to render, um but the user's only seeing a small amount of it, and it's not really performant to render, to create DOM elements and styling and layout and everything for a lot of stuff the user isn't seeing. So what libraries like fixed data table and React Virtualize and React Window do is they um they sort of cheat and mimic. Like, so if you imagine the simplest cases, you have a list that has you know thousands of items. At any given time, your browser screen is only big enough to show you know 10, Sure. 20, 50, some some small number. And so what these libraries typically do is they'll render that number plus a few more maybe, and then uh in response to user scrolling, they'll sort of move and update the content. And so the user has uh an experience that's really similar or maybe even indistinguishable, with some caveats like browser find and page search. But they have an experience that's very similar to if you rendered all 10,000 things. But um you don't lock the you know you don't waste time scripting and layout and stuff for those things. Um so React Virtualized and React Window, which are libraries I've written, are um just generic libraries for uh for doing this this windowing uh in video games. It's called uh occlusion. Um I'm really tired. Oh no. I'm blanking on the term, but it's it's a similar it's a similar technique. Occlusion?

[00:19:48] Michael Chan: Is that what you said?

[00:19:49] Brian Vaughn: Yeah. Yeah. It's basically like you have a whole 3D world, you might show us something, but really you only need to render what the camera is looking at at any given time. And also if something's behind a wall or some other scene, you can't see it, so don't waste cycles rendering it.

[00:20:01] Michael Chan: Sure.

[00:20:01] Brian Vaughn: It's very similar in a browser world, but with 2D space. So a list or a grid. Um, and uh yeah, these are just libraries to to make that easier. So with with these libraries, typically you tell it some basic information like here's um here's your size, you know, you're you're this width by height. Here's how many items you have to display, and here's something that will render an item if you give it an index. And then the library is responsible for doing all of the the sort of magic to make it fill the viewport, but then like not waste cycles rendering what's outside of the viewport.

[00:20:34] Michael Chan: Interesting. So uh was it UI fixed table? Is that what it or?

[00:20:38] Brian Vaughn: Fixed data table. Fixed data table.

[00:20:40] Michael Chan: That's right. And that came out in like 2003 or 2004? No, 2003. 2014.

[00:20:47] Brian Vaughn: Yeah, it was probably really early on. It's like the first React app, right? Yeah, that predates my time with React even. I was using Angular at the time. But that was early on.

[00:20:56] Michael Chan: So that was for tables, but React Virtualized is a little bit more generic, right?

[00:21:03] Brian Vaughn: Oh yeah, so yeah, so uh that's true. Um, fixed data table um really only like windowed in one direction. So like

[00:21:11] Michael Chan: Okay.

[00:21:11] Brian Vaughn: Y-axis. Um, virtualized and window both can window in 2 dimensions if you want. So they'll do grids, like uh Excel spreadsheet type things, and um they'll do the the windowing both uh on the X and Y axis. So um that's another thing that was important to Treasure Data because we would have tables of, you know, hundreds of thousands of rows, but also thousands of columns.

[00:21:34] Michael Chan: Yeah.

[00:21:34] Brian Vaughn: Uh and like you don't want to render thousands of columns, even of just a couple of rows, because it's on slower computers, it's just wasting the CPU cycles. So yeah.

[00:21:43] Michael Chan: Sure, sure. Interesting. So you have uh React Virtualized. Um where does React Window kind of spin out of that, or what's the relationship between those two libraries?

[00:21:54] Brian Vaughn: Yeah, they're very similar. Um I think I sort of burned out on React Virtualized uh personally because I was the primary maintainer, sometimes the only maintainer, for it, and it it got, you know, medium-level traffic, not like React-level traffic. Sure. Still a million downloads a month and a lot of GitHub issues and Stack Overflow, and I had everything set up to go to my inbox, and I would try to keep up every day. I would like sit down for an hour or two in the evening on my own time and like respond to questions. And I just burnt out a little bit, and I also, I made a mistake. In hindsight, this, you know, this was my first larger-scale open source personal project. And I made a mistake in hindsight of just accepting too many feature requests. And like once something's in your library, it's in. You know, the bar for taking it out is much higher than the bar for adding it. And so I have all of these like vaguely related things that like manage infinite scrolling and scroll syncing and keyboard navigation and all these things that like yeah, they're useful if you're doing, but like now I'm supporting them for every version. Yeah. And so basically I reached a point of burnout, but it's still like an area, a problem space that I'm passionate about. And I struggled for a while, like thinking I would like to- like knowing what I know now, I would do things differently. Also, I worked with Spencer a bit on the React Native team as he was rewriting like the the React Native list components, which do a very similar thing. Sure. So I I had some ideas in my head from like looking at what he did there. Learning what I've learned before, um and I thought, well, I could either do, you know, another major release of React Virtualized and all of the baggage that comes along with all the components in it that are not core and I don't want to support, but I don't want to rip them out in a major either.

[00:23:38] Michael Chan: Yeah.

[00:23:38] Brian Vaughn: I don't want to leave people stranded. So I found some people who were interested in doing some maintaining of React Virtualized and I thought, well, I'll just do- it started off as just a thought experiment, like, what if I were to make the same type of library but with no preconceived nothing? Like completely from scratch. I didn't even copy any code. I just like on a flight to visit my parents once, I just like started coding from scratch. And then there were a couple periods where I was like, this is ridiculous, what am I doing? And I'd like just leave it for a few weeks and then I'd go back. And then I ended up with an API that I thought was much nicer, like more ergonomic, easier to understand, also performed better because of, you know, at this point I'd been on the React team the core team for like a year and a half. And so I had better knowledge of just advanced techniques for things that I didn't know before. Um, and so I ended up eventually with React Window and I thought, well, now I have to decide, is this React Virtualized v10 or is this something different? And I asked Twitter and Twitter gave me a completely mixed feedback, 50% of like, do this. And so I reached out to Michael Jackson and I'm like, hey, you have a package name that I really like. You have a million of them, uh half a million of them are not being published to, this is one of them, can I have it? And I was like, if he says yes, I'm gonna publish there. If he says no, I'll do Virtualized. And he was like, well, I was kind of thinking of using it, but sure. So, uh yeah, it ended up just being that. And I thought like, it gave me, so so anyway, long story short, they both do a very similar thing. Virtualized is a little more full-featured, uh which could be good or bad. Window is definitely smaller, like uh faster, um I think more intuitive if you're doing brand new development. I would suggest it. But um some uh there are some people working on virtualized still that are uh that are awesome. And I think also some of the the patterns and stuff I've learned and implemented in Window are making their way into virtualized because the maintainers are pulling it in. So that's good too. Oh, cool. Hopefully that'll benefit. future releases of that. Yeah, and then I think uh React, we have some plans in React too to make this general type of library uh more powerful out of the out of the gate. So we have some ideas for some new primitives we could add to React for this type of component that would let it do some things that are you know traditionally not easy to do. There are two types of challenges, there are at least two types of challenges, um but there are two two of the challenges you face for windowed component. You want to make it uh feel as you want it to feel as normal to the user as possible, as similar to unwindowed content.

[00:26:15] Michael Chan: OK.

[00:26:16] Brian Vaughn: So some of that is like being responsive. Um you don't want the user to do a quick scroll and see like a bit of blank space where you didn't render fast enough. There's some challenges like that. But there's some more fundamental challenges of like accessibility. And this means like Things like uh uh the scroll cursor, which Ryan was just talking to me about and how like his Reach UI components are gonna handle that. That's hard, slash not even really truly like fully possible to support in a virtualized content. There's also like find in page. So you can tap into the Ctrl+F key sequence, but you can't tap into the browser's like find in page menu up top. There's no event. And also, even if you could, like what the heck does that look like? You've got that's gonna search both your windowed content, which you might not even have loaded on the client, like a lot of the data might still be remote. It's also gonna search the surrounding page content, which is like outside of your control. Um, so the cool thing is, we on uh the React team and some people from the Chrome team have been talking about some APIs uh down the road that would let windowed libraries tap into that event. Interesting. And like, there's still some stuff to be decided, but that's something that maybe we could do better support of down the road. Another one of the challenges that's specific to React that we can solve right now, we haven't, but we're gonna work on it soon, is um has to do with component state. So uh you know if you have component state or a function component with useState hook, which I can now talk about, what happens if that component is displayed in a list and then you scroll down and then the windowed list unmounts it and throws it away, and then you scroll back up and then it recreates it? Like that's a new instance, and so the local component state is gone. Oh wow. And so if that component state was managing something like a checkbox or some- anything, um, that's gone. And so this is a common question for windowing libraries, and the answer has always been, well, oh, you have to like lift that state up to an ancestor.

[00:28:13] Michael Chan: Okay.

[00:28:13] Brian Vaughn: And pass it down as a prop and pass a change handler down to be like- you have this like parent-child relationship. But React could help you with that. React could give you a primitive that says like, oh, this is a new type of component that that lists like that when components extend or something, and and we could watch and we could basically say, okay, React knows when your component was created, and React knows if you've had any state updates, and React knows when your component was unmounted. And so what it could do is just hold that updated state value for a little while, and if it gets remounted, just give you that as your initial state.

[00:28:46] Michael Chan: Oh, interesting.

[00:28:46] Brian Vaughn: So there's some stuff that we're talking about doing in the coming months to sort of make this a little more transparent, and then this would hopefully dovetail into work with the browser team longer term to make it more transparent on that side too. Um, so there's a lot of room for improvement hopefully in the next, you know, months to year or so.

[00:29:03] Michael Chan: That sounds really exciting. So to to bring it home, just to to visualize it for myself, like so if I'm in my email client and I'm using some type of windowing or something and I check, you know, I I want to archive some emails and I check the first 5 and then I scroll those out of screen and then maybe check you know, 22 and 26. If I scroll back up, given the state of things now, if I did not architect my app in precisely the right way, I could scroll up to find those first 5 unchecked again.

[00:29:35] Brian Vaughn: You could potentially, yeah. Um, now probably in the case like you're describing, if it's sort of like a form type thing like inbox, you probably have central state somewhere higher up. But there are cases where you wouldn't, and it can be confusing to people. But that's the general problem. It gives away the fact that- and it's not intuitive too, because you're just you're not used to thinking about that, and everything about windowing libraries kind of try to make you forget about that as much as you can.

[00:30:02] Michael Chan: Yeah.

[00:30:02] Brian Vaughn: So this is something that we could do to go a little farther, hopefully. And also React's got, you know, this- we talked a little Andrew talked a little bit about off-screen priority rendering with his tabs demo earlier today. We could do that in lists too. You know, we could, as a, as an author of React Window, I can't really do this, but as some, as React, I could do this. I could say like, oh, well here's a special window component. Um, I don't know how the API would look yet, but like it's got, you know, let's say 20 things that are on screen. So they're whatever priority that is when they update. But if I've got CPU cycles, it's got an infinite number of off-screen things. So I could render a few more down And then that would help with that first issue I mentioned where when you do a fast scroll, you can like- the browser, uh, I don't actually know how it works at a low level, but the browser sort of shifts around its processing so that if you have- if you just start scrolling, sometimes the scroll thread will be ahead of the scripting thread and then they'll catch up and sort of stay in sync.

[00:30:58] Michael Chan: Yeah, yeah. Um,

[00:30:59] Brian Vaughn: but if you- if React sort of pre-rendered off-screen stuff to get ahead of that initial scroll, this could this could go a long ways to like making the hiding the fact that we're windowing to making it feel more native. Um, it still wouldn't solve some of the accessibility problems like the the scroll uh index, but that's you know baby steps, I guess.

[00:31:19] Michael Chan: It it seems that as React takes on more of the responsibility of performance for React developers, that that it kind of creates a class of these problems where, like you said earlier with the profile, it's it's hard to stitch some of that information back together in a way that's comprehensible. And then with windowing, you can get into these weird states if you don't know exactly what you're opting into with windowing. Um, so I'm excited to hear that there's like some ideas on the horizon to to to sort that out even better.

[00:31:54] Brian Vaughn: Yeah, yeah, for sure. And You know, there's a tension between- we don't wanna- like, we're a small team, so we don't wanna take on too much, but also we don't wanna do something that stifles creativity and innovation. So I think with the windowing, for instance, I think it'll be important that we um that we add some primitives that React Window uses, but also other things could use. But there's this tension anyway between like not stifling innovation, but also the the things like this, the more that we own, the more that we could- support them as sort of first class in DevTools and like maybe give more insight. I don't know what this would look like for windowing, but with hooks or with Suspense cache, with things like that, we can give more insight into um sort of the state of, you know, uh well, the state of your application. It's harder to do, you can't really do that for third-party things. So there's a little bit of a tension there.

[00:32:42] Michael Chan: Interesting. So one thing that I always um try to ask people when I can is, So going back to the profiling thing, if someone is just getting into React, just getting into profiling, um what is some like low-hanging fruit that they could start incorporating into their development process and start looking for and better utilizing the tools that um React is giving us?

[00:33:08] Brian Vaughn: I'll take a stab at this, but I'm not sure if it's— uh clarify if I'm going the wrong direction. I think um Profiling is- it's like performance optimizing is- it's sort of as like, yeah, I don't want to say it's an art, but it's it's different. Like uh you can prematurely do it, um and this can hurt like the maintainability and readability of your code. So if you're just starting with React and if you're writing a small app, then like don't focus on this. If you're, you know, if you're working on a bigger app, if you're whatever, like I think I'll just say don't uh don't feel like you need to dive in and hyper-optimize everything. Sure. But let's say that you've um you're curious about your code or you have observed or you've heard that there's some performance problems or whatever and you start using the profiler. With any performance profiling, I think of just any just aside from React, in general, um it's important to sort of measure, take a baseline, make a change, and then compare. So um the talk I did today, I showed a similar thing with the profiler. Basically, record something in your app, do some things, stop, look at the data. Um if it gives you ideas for things to change, change them, and then repeat and see if you're if you're actually noticing a difference. Sometimes it's very unintuitive. Sometimes you make a change you think will help and it makes things worse. Sometimes it's just noisy, and if you profile 4 times, you'll just get a couple different results. But um but there are some types of things I think that are more um more objective. So for instance, um one of the things I showed today was um a component maybe that's like slow to render, it's expensive, and um it rendered even when the props didn't change. Yep. So we've we've had uh PureComponent mixin and PureComponent and um this like shallow compareUtils have been around in React for a long time. And we have been careful not to not to do it by default and also not to recommend you do it everywhere because sometimes it's overkill.

[00:35:08] Michael Chan: Sure.

[00:35:08] Brian Vaughn: But um sometimes it if your component is uh slow to render, then it can be a nice performance boost to not render unless its inputs change. So like DevTools will tell you very clearly um for each commit what rendered and you can see, oh, like it's gray, it didn't re-render, or it's got a color, it re-rendered, and then the color Whether it's like a cool blue color or a bright yellow color, will give you a sense of how long it took to render. So like this is just information I hope that's like in your face enough that you can look and say, well, I know it's rendering and now I'll use like my human brain to decide if it's important enough to to deal with. Because you have a lot of factors to consider. So like the DevTools, I uh we were intentional not to be too prescriptive. Um we want to basically give you the information and then let you decide what it means. Because on the one hand, typically you're profiling with a development build of React, which is slower. But on the other hand, typically you're profiling on a 2017 or 2018 MacBook Pro, which is way faster than what most people are using. Yeah. And if you're in the Bay Area, you've got like good internet probably, and like you're you're spoiled and you're out of touch. So it's like important to remember that just because it's 60 frames per second for you, it's it's not gonna be for everyone. So um the profiler shows you everything, as a relative scale, which means if your app is really fast, you'll still see some yellow saying- Oh, nice. This is the slowest thing, and you have to decide with your brain, like, is it really slow? If your app is really slow, you might see some things that are green and blue that are also still slow. Um, but yeah, so basically I think like one of the low-hanging fruits like I showed today is just sort of memoization at the component level. So like use memo or use the new memo utility or use PureComponent, whatever version of React you're on. Another uh potential big performance win, um I did show an example of this today. I showed another example of this in the deep dive profiler YouTube video that I I did a couple of weeks ago that's up on the React blog. Um but it's just memoization in general. So if you're doing a filter or uh even a map, something that's expensive on a lot of data, um don't do it unless your inputs change. You know, use a library like memoizeOne that has a cache size of 1, so so you don't have to worry about memory growing unbounded, but just like don't recompute expensive things. Um, but again, I want to couch that by saying if you're you know mapping an array of 10 and it's like never gonna be more than that, like don't you know.

[00:37:30] Michael Chan: Don't worry about it.

[00:37:31] Brian Vaughn: Yeah.

[00:37:31] Michael Chan: Nice. I really like that that idea. The the the profiler allows you to have an additional lens into your into your code and the way it's executing. I remember when Firebug first came out and I was actually able to see the box model for the first time. It totally changed the way that I developed. Yeah. And it it seems to me, after seeing your demo today, that this profiler allows you to have to hold less in your head about how React is rendering. And you can actually see, like, oh, these these 5 list items didn't change and these 5 did. Like, that's perfect. That's exactly what I want it to look like. And if it's not, you have new tools like useMemo. Or sorry, uh React.memo?

[00:38:13] Brian Vaughn: Memo, useMemo, they're both they both exist. useMemo is the hook.

[00:38:16] Michael Chan: Use one of the the memo options to um to to to improve that.

[00:38:22] Brian Vaughn: Yeah.

[00:38:23] Michael Chan: Awesome. Uh do you have any parting advice or wisdom for us?

[00:38:27] Brian Vaughn: Wisdom, no. Yeah, I mean, I guess uh I would just say, um you know, we've released a lot of stuff in the last couple of weeks, but especially this week. Um, we're really excited about it, but we're also um experimenting with a lot of it, so I would ask everyone to sort of be patient with us, give us your feedback, let us incorporate it. We're still learning, we're we're trying things out. Some things will stick, some things won't. That's why some of the APIs are unstable. So please play with them, but also know that they're unstable, so they might change. Don't feel in any way that you have to go out and rewrite your app to use any of the new stuff, because the old stuff will still be supported as far forward as we can see right now. We're not planning on deprecating. We have 50,000 or 60,000 React components at Facebook. A lot of them are class components. Yeah, we just can't get rid of them. So don't let it stress you. Hopefully it excites you, but don't don't be stressed out about it. If you can upgrade to at least 16.5 in the future at some point when you'll get the profiler, uh that'd be cool. I would love to hear anything that you're able to learn from the profiler, because if I hear enough common, I found this patterns, then I can at least document it. Maybe I could find a heuristic there that I could surface in the profiler. But like share your share your successes and your pains with us on Twitter, because we do like hearing that. And uh yeah, that's it.

[00:39:48] Michael Chan: Awesome. Well, thanks so much. This was extremely informative for me. I appreciate you talking about uh all of that stuff and introducing us to the new profiler.

[00:39:57] Brian Vaughn: Cool. Thanks a bunch.

[00:39:58] Michael Chan: Yeah, thanks. Thanks again to this week's sponsor, Linode. Don't forget, if you visit linode.com/react and use the promo code REACT2019, you will get a $20 credit. Just visit linode.com/react and use the promo code REACT2019. Thank you for listening to this episode of React Podcast. This show wouldn't exist if you weren't there to listen. It also wouldn't exist without Spec.fm, a podcast network built to help designers and developers level up. If you like React Podcast, you can find other shows like it on Spec.fm. Thanks specifically to Mikael Delport, who produced and edited this show. If you find this show fun, useful, or helpful, please consider leaving a review on iTunes, Spotify, or wherever you catch your podcasts. They'll help other developers and designers find the interviews and news that we cover here. And finally, you can get in touch with us on Twitter @reactpodcast or spectrum.chat/reactpodcast. As always, I'm Chantastic. Thank you for listening. We'll be in your ears again next week. Thanks for listening.