React's Secret Weapon: Leveraging Concurrent Features for Top-Notch Performance

Rate this content
Bookmark

Discover how React's often overlooked concurrent features like startTransition, useDeferredValue, and Suspense can transform your application's performance and responsiveness. Learn practical techniques to prioritise critical updates, gracefully handle loading states, and create fluid user experiences even with complex data interactions.

This talk has been presented at React Advanced 2024, check out the latest edition of this React Conference.

FAQ

The speaker started their journey into frontend development in 2017.

The useTransition hook allows developers to assign priority to a state update, distinguishing between high-priority and low-priority tasks.

The task scheduler prioritizes tasks and executes them, yielding control back to the browser to handle high-priority tasks like user interactions.

Common use cases include handling real-time search inputs, seamless tab or component switching, rendering large lists, and ensuring smooth animations and transitions.

The talk is about concurrency within React and how it can be used to improve application performance.

The two phases are the render phase and the commit phase.

The main concurrent APIs introduced in React 18 are useTransition, useDeferredValue, and extended Suspense for data fetching.

The speaker is a software engineer currently working at Spotify.

The useDeferredValue hook defers the updates of a non-critical value on the UI, whereas useTransition assigns priority to a state update.

Concurrency in React is the ability to manage multiple tasks simultaneously, such as rendering the UI, responding to user input, and processing network requests without blocking the main thread.

Dara Olayebi
Dara Olayebi
25 min
25 Oct, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
I'm going to be speaking about concurrency within React and how you can use it to improve performance within your applications. Concurrency is crucial for optimizing responsiveness and ensuring a good user experience in complex applications. React breaks up the rendering process into render and commit phases, allowing interruption of the render phase for prioritizing user interactions. React's task scheduler handles tasks in small chunks, ensuring a fluid user experience and responsiveness. High-priority tasks include user interactions, while lower priority tasks involve fetching data or rendering long lists. React introduced new hooks, useTransition and useDeferredValue, for handling state update priorities. The deferred value hook allows for deferring the updates of non-critical UI values, while Suspense for Data Fetching improves the performance of data fetching in React applications. Concurrency is useful for handling real-time search inputs, seamless tab or component switching, rendering large lists, and animating transitions. However, there are drawbacks to concurrency, such as potential delays in non-urgent updates and limited benefits for expensive components. Concurrency may add CPU strain and requires an understanding of React's background behavior for effective debugging. Use suspense on parent level components for data fetching. Performance profiling can help test the effectiveness of transition hooks.

1. Introduction to Concurrency in React

Short description:

I'm going to be speaking about concurrency within React and how you can use it to improve performance within your applications. Today's agenda includes a background on the performance problem, concurrency under the hood in React, the main concurrent APIs introduced in React 18, use cases, and drawbacks. Concurrency is crucial for optimizing responsiveness and ensuring a good user experience in complex applications.

I'm happy to be here. Thank you for being here. Thanks Natalia for the introduction. I'm going to be speaking about concurrency within React and how you can use it to improve performance within your applications. And yeah, I'm a software engineer currently working at Spotify.

Before I get into the talk itself, just a bit of background on my journey and how I got into frontend. So it started around 2017. I did study a computer science course or like a heavily tech related course, but I did build a website as part of my degree and absolutely hated it. But I found myself learning HTML and CSS after graduation. And then I did that for a bit and finally got my first role in tech in 2018. And this was basically my introduction into frontend frameworks and more specifically React. And then I worked there for a bit and moved on to a crypto trading startup where I was working more heavily with just React. And then 2022 started a full stack role, which is my first full stack role at Spotify. So I actually work more with backend these days now, but I work Java on the backend and React on the frontend.

Okay, enough about me. So today, here's a quick agenda, what I'm going to be talking about. Starting with a bit of background on the performance problem. So why are we talking about concurrency in the first place? And then we'll talk a little bit about concurrency under the hood. So how React actually is, how React achieved concurrency. We'll look at the main concurrent APIs that were introduced in React 18. I'll talk through some use cases briefly and then some drawbacks because everything comes with a drawback. All right. So if you think about the applications you use on a day to day basis today, Instagram, Twitter, those applications rely on giving immediate feedback to the user, right? When I scroll through my Instagram feed, I'm getting feedback immediately. And the applications that we build today have become increasingly complex, right? Compared to many years ago. So there's more data that these applications have to handle, which means more renders, more computation. And all of this essentially means there's a higher chance of an increased burden on the performance of these applications. So essentially, this is kind of where concurrency comes in. It's crucial to optimize for responsiveness to ensure that the user experience on these applications are as good as possible.

So here. And on to the main subject.

2. The What and How of Concurrency

Short description:

Concurrency is the ability to manage multiple tasks simultaneously without blocking the main thread. React breaks up the rendering process into render and commit phases. In the render phase, React compares the existing DOM to the virtual DOM, calculates the diff, and determines the necessary changes. The commit phase applies these changes to the DOM, allowing interruption of the render phase for prioritizing user interactions.

The what and the how of concurrency. In a nutshell, concurrency is the ability to manage multiple tasks simultaneously, such as rendering the user interface, responding to user input, processing network requests, and doing all of this without blocking or slowing down the main thread. So let's look a bit into the background of this and how React actually achieves this.

So top image, synchronous, bottom image, concurrent. And the rendering process in React is broken up into two phases. So there's the render phase, and there's the commit phase. So if you look at the first image, you notice that the render phase, which is where React will compare the existing DOM to the virtual DOM, calculate the diff, and basically do this computation and determine what changes need to be made. And then the commit phase is where React will actually apply these changes to the DOM.

So if you look at the first image, it's just one long continuous block of rendering, right? And then we have the commit, and then we have these tasks. And what that means, typically, pre-React 18, is that the render phase could not be interrupted. So when I load a component or a page, React has to complete that render phase before the user can actually interact with the web page. So the commit phase, as you can see, basically makes this process interruptible. So if you look at the bottom image, you notice that this render phase is broken up into small chunks and essentially, React has created something quite intelligent, where instead of rendering everything all in one block, it can actually interrupt this render phase and the user can prioritize user interactions between the render phase.

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 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
Understanding React’s Fiber Architecture
React Advanced 2022React Advanced 2022
29 min
Understanding React’s Fiber Architecture
Top Content
This Talk explores React's internal jargon, specifically fiber, which is an internal unit of work for rendering and committing. Fibers facilitate efficient updates to elements and play a crucial role in the reconciliation process. The work loop, complete work, and commit phase are essential steps in the rendering process. Understanding React's internals can help with optimizing code and pull request reviews. React 18 introduces the work loop sync and async functions for concurrent features and prioritization. Fiber brings benefits like async rendering and the ability to discard work-in-progress trees, improving user experience.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
How React Compiler Performs on Real Code
React Advanced 2024React Advanced 2024
31 min
How React Compiler Performs on Real Code
Top Content
I'm Nadia, a developer experienced in performance, re-renders, and React. The React team released the React compiler, which eliminates the need for memoization. The compiler optimizes code by automatically memoizing components, props, and hook dependencies. It shows promise in managing changing references and improving performance. Real app testing and synthetic examples have been used to evaluate its effectiveness. The impact on initial load performance is minimal, but further investigation is needed for interactions performance. The React query library simplifies data fetching and caching. The compiler has limitations and may not catch every re-render, especially with external libraries. Enabling the compiler can improve performance but manual memorization is still necessary for optimal results. There are risks of overreliance and messy code, but the compiler can be used file by file or folder by folder with thorough testing. Practice makes incredible cats. Thank you, Nadia!

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
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 🤐)
Concurrent Rendering Adventures in React 18
React Advanced 2021React Advanced 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.
Getting Started with Suspense and Concurrent Rendering in React
React Summit 2020React Summit 2020
125 min
Getting Started with Suspense and Concurrent Rendering in React
Featured Workshop
Maurice de Beijer
Maurice de Beijer
React keeps on evolving and making hard things easier for the average developer.
One case, where React was not particularly hard but very repetitive, is working with AJAX request. There is always the trinity of loading, success and possible error states that had to be handled each time. But no more as the `<Suspense />` component makes life much easier.
Another case is performance of larger and complex applications. Usually React is fast enough but with a large application rendering components can conflict with user interactions. Concurrent rendering will, mostly automatically, take care of this.
You will learn all about using <Suspense />, showing loading indicators and handling errors. You will see how easy it is to get started with concurrent rendering. You will make suspense even more capable combining it with concurrent rendering, the `useTransition()` hook and the <SuspenseList /> component.
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
React Performance Debugging
React Advanced 2023React Advanced 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
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 🤐)