React Performance Debugging Masterclass

Rate this content
Bookmark
Github

Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).


Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.


Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.


(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)

FAQ

The React Performance Workshop is a session designed to discuss and address performance issues in React applications, focusing on identifying and resolving slow app interactions.

The workshop was conducted by Ivan, a Google DevOps expert and web performance engineer with five years of experience working with companies like Google, CNBC, and Framer.

During the workshop, tools such as React DevTools, ConsoleTime API, MemLab, and custom scripts like 'useWhyDidYouRender' were utilized to diagnose and optimize performance issues.

Common strategies include using React Profiler to identify costly components, implementing useMemo and useCallback to prevent unnecessary renders, and optimizing state management with tools like Redux or Zustand.

Using React context excessively or improperly can lead to performance issues as it might cause unnecessary re-renders if not managed carefully. It's important to optimize context usage by minimizing the number of context providers and leveraging memoization.

Ivan Akulov
Ivan Akulov
170 min
19 Jun, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Workshop on React Performance covers various techniques and tools to analyze and optimize app performance. It includes using Chrome DevTools' Performance and Profiler panes, analyzing CPU activity and function calls, identifying expensive renders and components, debugging with 'why did you render', and optimizing React code and effects. The workshop also discusses the use of ConsoleTime API for profiling, optimizing the state manager, and analyzing memory performance. Overall, it provides valuable insights into improving the performance of React applications.

1. Introduction to React Performance Workshop

Short description:

Welcome to the React Performance Workshop. Today we'll talk about approaching React performance issues and analyzing a slow app. We'll use Chrome DevTools' Performance pane to record and analyze app performance. Let's start by looking at the CPU row to identify where the app is slow. Then we'll zoom into a recording to analyze the main pane during CPU spikes.

♪♪ Welcome to the React Performance Workshop for React Summit. I'm very glad to meet all of you and to talk to some of you already. So I'm Ivan, I'm a Google DevOps expert. I'm a web performance engineer. I've been working on performance for five years at this point. I've worked with companies like Google, CNBC, Framer, etc. And today we're going to talk about the ways to approach react performance issues when your app is slow and you need to figure out why. But you're not really sure about what the next steps to take. Which was pretty much me at the beginning of my career.

And so to start... Oh yeah. Work stuff. So the workshop lasts for three hours, right? We'll have 10 minute breaks every hour. We have time for Q&A in the end. If you have any questions in the process, feel free to... Actually, sorry, I think this meeting is a bit too big for Ani to ask. So yeah, you did not see that. If you have any questions, feel free to ask in the chats or give them for the Q&A at the end. And also, in case you're the person who likes to keep notes, there's a link, bit.ly dash Ares for notes, which, if you go to that link, you'll see this document called React Performance Workshop, Collaborative Notes. And here you'll find the link to this doc, to the Google rep or to the Discord chat, where I'll drop the links after this workshop. And also, if you keep any notes or if you get any questions that you want to ask later but don't want to forget, feel free to keep them in the notes here so that everyone can collaborate on the notes and everyone benefits.

And so to start, let's go and look at a slow app right away. So if you open this repository and if you clone this repository, which I already have cloned, and you open the notes directory, and you install the dependencies and then run yarnstart, you'll get this basic note taken up. You, like, if you attended... If you attended the talk that I was giving on rec time, it's about rect concurrency. You might have seen this, which is not a coincidence, but we're not going to be talking about rect concurrency today, we're going to be talking about other issues. And this app has performance challenge. Specifically, if you create a few hundred nodes, like 500, 600, 700, and then open any of these nodes, and then try typing to the editor, the app would feel pretty laggy, pretty slow. So here I'm typing to the editor in the app, and the app feels laggy. Now you can't really see that, right, because it's my laptop, it's me typing, it's me feeling this. You can't really notice it being slow. So one thing I'm gonna do for you to see when the app is slow is I'm going to go to DevTools, More Tools, Rendering, and I'm going to click the Frame Rendering Stats checkbox. So when I click Frame Rendering Stats checkbox, I get in the top left corner this thingy, which basically shows me when the Main Thread is busy or idle. So right now, if I do something quick, like scroll this list, I could see that my frames per second rate stays high, and there are some reds and yellow lines appearing, which means that the Main Thread was busy with some work for a very short period of time. But if I try typing into the editor, you could see how the frames per second rate is falling immediately upon every key press. And you can see how the Main Thread starts to get filled with reds. Which basically means the Main Thread is blocked. So even if I just type a single letter, you could see the Main Thread getting blocked for some prolonged period of time. Or if I just type multiple letters at once, I just typed a bunch of letters and the app was so busy, it didn't even update the frame rates. Chart, is this a chart? Maybe this is a chart. But anyway, so we have a performance issue. I'm typing to the editor and the app is slow. Now, whenever I have a performance issue, the first thing I always do is I go to ChromDevTools, I open the Performance pane, and I try to record whatever's happening in the app during this performance issue using the Performance pane. So what I'm going to do now is I'm going to open the Performance pane, which I already have open, I'm going to click Record, I'm going to type into the editor, but not a bunch of letters at once, like I just did. Because that's going to trigger a lot of JavaScript, supposedly or whatever, whatever it is that's slowing the app. But just one letter and then wait a bit, another letter and then wait a bit again, and another letter, and then stop the recording. And once I do this, I would get this performance recording that will show me everything that's been happening in the app during the recording, while I was typing on these keyboard buttons. So if you see this for the first time, this might be a little overwhelming, because there's like a lot of stuff happening here. But the only actual two areas that we need to focus are the CPU row, which shows when the app, when the main thread was busy, and the main pane, which shows what exactly was happening in the app when the app was busy. So now the first thing I'm gonna look at, so I've just recorded, I've just did performance recording, right? So the first thing I'm gonna look at to try to figure out what's going on in the app is I'm going to look at the CPU row, and I'm going to try to figure out what exactly, or figure out where exactly the app is slow. And I can see, like, this is pretty easy to see, right? When the CPU row is empty, that means the app was idle. When the CPU rows filled with some color, like yellow in this case, for JavaScript, this means the CPU was busy running some code. So in this case, I see three spikes of CPU activity, which, I guess, corresponds to me typing into the editor. So what I'm going to do next is, I'm going to zoom into one of the recordings and switch to the main pane and try to figure out what exactly is happening in the main pane during the spike of CPU activity. And so let's take a look. So we have a spike of CPU activity. The spike of CPU activity took 215 milliseconds. In the spike of CPU activity, we have two rectangles that indicate what's been happening at the very topmost level. So we had a task, a single task, which means a single and interrupted period of time when the browser was processing something. This task took 200, sorry, 215 milliseconds. And during this task, we get two events. One, the key down event, which took just three milliseconds to handle. And another one, the key press event which took 212 milliseconds to handle. So the first one is probably not super important because it's, so first of all, look so far. So far so good, right? This matches what they get in the app. I typed into the editor, we got the key press event, key down event. So the key press event is cheap. I'm going to ignore it for now, but and I'm going to focus on the key press event, which is, sorry, the key down event is cheap.

2. Analyzing Key Press Event and Function Calls

Short description:

The key press event is the expensive one. It triggers the text input event, which then triggers the input event. These events lead to multiple function calls, mostly from Ragdoll development and the React core.

So the key press event is cheap. I'm going to ignore it for now, but and I'm going to focus on the key press event, which is, sorry, the key down event is cheap. I'm going to focus on the key press event, which is the expensive one. So the key press event triggered. The key press event in turn triggered the text input event. The text input event in turn triggered the input event. The input event triggered some function call, which seems to be mostly this textAreaInputPol function, which is something, I'm not going to dig into that now. I'm just taking a high level look. And then the runMicroDosks rectangle, which took 209 milliseconds, so still most of the time. So runMicroTasks triggered another function call, which in this case seems to be coming from Ragdoll development. And that function call triggered another function call, and that function call triggered another function call, and then that function call triggered another function call. And what I see here is a whole lot of text input events function calls coming from Ragdoll development, coming from the React core.

QnA

Watch more workshops on topic

Concurrent Rendering Adventures in React 18
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Web3 Workshop - Building Your First Dapp
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a new web framework from the creators of React Router that helps you build better, faster websites through a solid understanding of web fundamentals. Remix takes care of the heavy lifting like server rendering, code splitting, prefetching, and navigation and leaves you with the fun part: building something awesome!
Don't Solve Problems, Eliminate Them
React Advanced Conference 2021React Advanced Conference 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Humans are natural problem solvers and we're good enough at it that we've survived over the centuries and become the dominant species of the planet. Because we're so good at it, we sometimes become problem seekers too–looking for problems we can solve. Those who most successfully accomplish their goals are the problem eliminators. Let's talk about the distinction between solving and eliminating problems with examples from inside and outside the coding world.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
Few developers enjoy debugging, and debugging can be complex for modern web apps because of the multiple frameworks, languages, and libraries used. But, developer tools have come a long way in making the process easier. In this talk, Jecelyn will dig into the modern state of debugging, improvements in DevTools, and how you can use them to reliably debug your apps.