Inside Fiber: An Overview of React's Reconciliation Algorithm

Rate this content
Bookmark

With React 16.0, Facebook has released an update to the React core reconciliation algorithm which was named ""Fiber"". Fiber allows React to break the limits of the call stack and pause/start rendering work at will.


In this talk, we will explore why Fiber was necessary, cover some of the internal implementation details of Fiber, and see Fiber in action with React's experimental Concurrent Mode.

20 min
14 May, 2021

AI Generated Video Summary

React Fiber is a reconciliation algorithm introduced in React 16 to address laggy input fields and heavy rendering. The old stack reconciler caused a laggy experience by re-rendering the entire subtree immediately. React Fiber solves this by breaking work into incremental units and assigning priorities. It introduces concurrent mode to make apps responsive and adaptable. The useDeferredValue hook is commonly used to keep the interface responsive by rendering components immediately and others at a later time.

1. Introduction to Fiber and Demo

Short description:

Hi, I'm Elad Zamek, a software developer at Viva Systems. Today, we'll talk about Fiber, React's newest reconciliation algorithm introduced in React 16. We'll cover what React had before Fiber, why Fiber was necessary, and what we can achieve with it. Let's start with a quick demo. I have a simple app with an input field and a list. Whatever I type is rendered in the list. The list component waits for three milliseconds to simulate a heavy render.

Hi everyone, my name is Elad Zamek and I'm a software developer at Viva Systems in Toronto, Canada. Viva provides cloud-based products for the global life sciences industry and I currently work there as the lead UI developer for the Safety AI product.

Today, we'll be talking about Fiber, React's newest reconciliation algorithm that was introduced in React 16. We'll talk about what React had before Fiber, why Fiber was necessary, and some of the things we can achieve with Fiber. We have a lot to cover so let's get started.

I'll start with a quick demo here and as you can see, I have a pretty simple app that has an input field. Whatever I type into the input field is then being rendered. We have 80 elements in a list that's being rendered with the value that we typed into the field. If I type in a lot, we're going to get 80 elements with a text a lot. My slow list is a component that's being rendered. If I go into it, we can see it returns us an array of 80 elements of the list item component which is a component that waits for three milliseconds and then returns an LI element with the children prompt which is essentially the text that we passed. The reason that I'm waiting here for three milliseconds is just to simulate a component that's heavy that takes a long time to render because it's very complex.

2. Laggy Input Fields and Heavy Rendering

Short description:

When typing into the input field, there is a noticeable lag before the value is rendered in the list. This is due to a complex heavy component, MySOLIST, that renders as the typing occurs. Many applications experience this issue with laggy input fields.

Now, if I type something into the input field, so let's say I'm going to type React conference, typing and stop typing now and then I can see the value in the input field and in the list. So I'll do that again, React conference, stop typing now, and then I see it. So I hope that you noticed that lag that I had. It was like about a second or two, between the moment I finished typing React conference into the input field, and the moment I saw the value being populated in the input field itself and being rendered on the list. And that's because when I typed in the value, I had another component, the MySOLIST component that rendered as I was typing it. And I'm sure that like most of you at least, I've seen this type of issue happening in your applications where you have like a laggy input field because there is another complex heavy component that needs to be rendered based on the value that you have in the input field or something similar to that.

3. Understanding the Laggy Experience

Short description:

Let's take a look at our app and understand why it's laggy. The stack reconciler, used in React 15 and earlier, is a purely recursive algorithm. Whenever the app's state changes, the entire subtree is re-rendered immediately. This causes everything, including the slow list, to render, resulting in a laggy experience.

So let's take a look at our app just so I can understand why this is happening. And we're gonna look at it in the form of a tree connecting all of the components and see what's happening. So the stack reconciler is the implementation powering React 15 and earlier. I won't go too much into details about how this algorithm works because we are here to talk about the Fabry algorithm. But what's important to know is that for example, the state of app has changed. React will traverse this component tree recursively asking each component the way, what are you rendering to based on that state change. Then based on the result of the render function, it would create a virtual DOM and updated DOM with that result. So this reconciliation algorithm is a purely recursive algorithm. An update results in the entire subtree being re-rendered immediately. While this works well, it has some limitations. You can see here that the input element in blue and that's the one where fast response is important to us. We want the user to see what he or she types right as he or she types it. However, since the typing updates the state of the entire app component, everything renders including that red slow list and that's why everything seems so laggy.

4. Issues with Stack Reconciler

Short description:

With the old reconciliation algorithm, React couldn't break the work into incremental units, causing frames to drop and degrading the user experience. If React takes more than 16 milliseconds to render something on the screen, the browser will drop that frame, resulting in a laggy experience. JavaScript being single-threaded can block the browser from doing anything else, leading to jank and negatively impacting the user experience.

So what is really wrong with this stack reconciler? I have a quote here from Andrew Clark who's one of the team members of the React core team at Facebook. In UI, it's not necessary for every update to be applied immediately. In fact, doing so can be wasteful causing frames to drop and degrading the user experience. Now, with the old reconciliation algorithm, we couldn't break the work into incremental units. If React was going to walk the entire tree of components synchronously and perform work for each component, it may run over 60 milliseconds available for an application code to execute its logic.

Now, what do we mean when we refer to 60 milliseconds and why is this a problem with the recursive approach? Well, typically for a video to feel smooth and instantaneous to the human eye, the video needs to play at a rate of about 30 frames per second. Anything higher than that will give an even better experience. And this is actually one of the prime reasons why gamers prefer a higher frame rate for first-person shooter games where precision is very important. So having said that though, most devices these days refer to their screens at 60 frames per second, or in other words, with 60 frames per second, a new frame is displayed every 16 milliseconds. This number is very important because if the React render takes more than 16 milliseconds to render something on the screen, the browser will drop that frame and the user would get a lag experience similar to what we saw earlier. I don't know if you've ever seen a video that was created using 12 FPS or 10 FPS or 20 FPS, but it's like not as smooth as 30 and definitely not as 60 FPS.

Another thing that's important to note is that JavaScript is a single threaded language. And because JavaScript code is executed in one thread, only one line of JavaScript can be run at any given time. The same thread is also responsible for other document life cycles, like layout and paint. And this means that whenever JavaScript code runs, the browser is blocked from doing anything else. If React is going to walk the entire tree of components synchronously and perform work for each component, like we said, it may run over that 16 millisecond that's available. And because browsers only have those 16 milliseconds to do all the work, in order to run at 60 FPS, reacting to each of those events can completely block your JavaScript application when we fail to meet that 16 millisecond budget. That's essentially when we see the content of your web application jotters on the screen. It's often referred to as jank. Again, what we saw in our demo. And of course, it negatively impacts the user experience.

5. Introducing Concurrent Mode in React

Short description:

To make React faster, we need to solve two problems: long-running tasks causing frame drops and different tasks having different priorities. Introducing concurrent mode, an experimental feature in React, helps apps stay responsive and adapt to device capabilities. While not recommended for production, it's being used at Facebook. Check the official React docs for more details.

Now you might think, why not make React faster? Well, the problem in the demo that we saw, for example, was that in updates, such as the user input is stuck behind larger updates, such as the complex component tree, that was that slow list. That's user code, it's not part of React itself. So with everything I explained so far, we can formulate two problems that we have to solve in order to get two more responsive user interfaces. First one is that long-running tasks cause frame drops. And we need to make sure all of our tasks are small and can be completed within a couple of milliseconds so that we can run them within one frame. And then the second one is that different tasks have different priorities. So let's introduce concurrent mode. It is experimental and it's a set of new features that help React apps stay responsive and gracefully adjust to the user's device capabilities and network speed. So because it is experimental, things may change. It's not recommended to be used in production just yet, except for at Facebook where it is being used in production. And if you want more details, I would highly recommend checking out the official React docs.

6. Concurrency in JavaScript

Short description:

JavaScript being single-threaded can block the browser from doing anything else, leading to jank and negatively impacting the user experience.

And we just noted that JavaScript is single-threaded. So how can it possibly be concurrent? Well, it's mostly about conceptual threads or in more accurate terms, cooperative scheduling or cooperative concurrency. Essentially, what all these fancy terms mean is that once the thread is given control, it continues to run until it explicitly yields control or it blocks. So what does it mean? Let's take version control systems as an example. Before version control systems, as you can see on the right, if we wanted to work on a feature, you had to make sure nobody else was touching the files you were touching, you had to let everyone know we were working on these files. Essentially, we're blocking everyone else from making changes to these files since you were working on them. And this can be thought of as a synchronous process. When version control systems came along, it would just create a feature branch, do your work and then merge back. And this can be thought of as an asynchronous process.

7. React Fiber and the UseDeferredValue Hook

Short description:

React Fiber enables React to take advantage of scheduling and prioritization, allowing work to be paused and resumed later. It assigns different priorities to different types of work and allows for the reuse or removal of previously computed work. The useDeferredValue hook is commonly used to keep the interface responsive by rendering components based on user input immediately and rendering others at a later time. A Fiber is a JavaScript object that contains information about a component, its input, and its output. The reconciliation phase of the Fiber algorithm is interruptible, allowing for pausing and resuming, while the commit phase is not. During the initial render, each React element is converted into a Fiber and placed into a tree called current.

So React Fibre essentially eliminates the need to process updates in a synchronous recursive way. Instead, it enables React to take advantage of scheduling and prioritization, and that enables us to pause work and resume it later, to assign different priorities to different types of work, and reuse previously computed work or throw it away if it is no longer needed. So for example in our demo, React could keep the input field responsive to the user, meaning the user would see what he or she was typing right away since it would have a higher priority than the list of elements that needed to be rendered. Then when the user doesn't do anything, meaning doesn't block the main thread, React can render and display that list of elements.

So, that sounds cool, right? Well, let's see it in action. This is the same app that we had before. The only difference is that we imported the use deferred value hook here, and we're passing it the text state variable along an object with the timeout MS set to 2000. That's 2000 milliseconds, which is equal to two seconds. Now, we're passing the text state variable to our input field, because we wanted to be up to date with what we're typing, but to the MySlowList, we're passing that deferred text variable, because, well, you'll see. Now, let me type React Conference again. And you can't really see that, but as I'm typing, I'm seeing the characters being typed into the field. But the list is being left behind when it comes to what it renders. I'll do it again. See, finished typing, and now the list is slowly catching up on the rendering, right?

So the useDeferredValue is a hook that wraps a prop or state value and receives the maximum deferred time. This essentially tells React, it's okay for components depending on this value to be rendered at a later time. And it's commonly used to keep the interface responsive when you have something that renders immediately based on user input, just like in our demo here. So for example, with this code, it essentially says we need to show the text in the input field as soon as the user types it. We need to see exactly what they type, but when it comes to the list, it's okay if it takes up to two seconds to catch up on what it needs to render.

So let's understand what a Fibre object is, not the algorithm, the objects. So a Fibre is also a JavaScript object that contains information about a component, its input and its output. And it also corresponds to an instance of a component. There's quite a lot of fields on FibreNode. So this is not a comprehensive list by any means, but just to give you a general idea, you can see there's information about the component children, if it has any siblings, a pending updates type, pending props, pending state, work priority, and a bunch more. So when it comes to the Fibre algorithm, it has two phases, reconciliation and commit. Now, the reconciliation is interruptible, meaning we can pause and resume it whenever we want, and the commit is not interruptible, meaning once we start it, we can't stop it. We have to let it finish. So during the initial render, each React element that's being returned from the render function call is being converted into a Fibre. And when a React element is converted into a Fibre node for the first time, React uses that data, that Fibre that was created from the call. Essentially the React element is being converted to a Fibre, and then that Fibre is being put into a tree that is called current. So here you can see a specific example for the input element, but this, of course, happens for all the other nodes in the current tree as well.

8. React Fiber and the Reconciliation Phase

Short description:

React updates the DOM in one go, ensuring consistency. During the reconciliation phase, React builds a work in progress tree based on updates, containing the most up-to-date states and props for each component. This tree is not yet visible to the user. React processes components asynchronously, pausing and resuming based on available time and higher priority events. The second phase, the commit phase, is always synchronous.

React element being converted to a Fibre, which is being used to construct the current tree. And this tree is essentially, it reflects the current state of the UI that needs to be rendered. And it will be rendered, right? And the first render when we load our application.

Now let's talk about updates. When React starts working on updates, so, for example, a state change, it builds a work in progress tree that reflects the future state to be flushed to the screen based on that update. So as you can see in this case, it has the same fibres as the current tree. But let's say with this input element here, we can see on the left with the current tree, there's an effect tag of zero, meaning there's no update and the value is an empty string. But as we type, so let's say if we type alive, the first letter is E, we're gonna get a fibre with an effect tag of four, meaning there's an update pending and the value that it's gonna have is E. So all state and props update work is being performed on fibres from the work in progress tree. As React goes through the current tree for each existing fibre node, it creates an alternate node and all these nodes constitute the work in progress tree. The reason we need the work in progress tree is because we don't want to make changes to the DOM while we're computing the changes, unlike in the stack reconciler. Again, this slide just shows an example of how our input element is updated, but the same happens for all of the other fibres as well that have any updates. So the work in progress tree contains all of the most up-to-date states and props for each component, but it hasn't yet been flushed to the screen, but wait, does it contain all of the most up-to-date information on each component? Well, not really, because if you remember in our demo, we told React it's okay to delay the calculation of the slow list by at most two seconds. That means that other calculation work, like for our input element, is going to be prioritized and executed by the main thread, while other lower priority work, which is the mysoloist component in this case, will be queued for later because it has a lower priority. Only after the high priority work is done and displayed on the screen, only when we're done typing, because that's the high priority work, then React will move on to the lower priority work and calculate it. And again, that's the mysoloist component. So React has processed the app component and its children, except for mysoloist. And now it's done with the render phase, the reconciliation phase. On the right is the new tree that needs to be flushed to the screen. It can be processed immediately after the render phase or the reconciliation phase, it's the same thing, or picked up later when React is given time by the browser's main thread. In our case, since the two seconds haven't passed yet, let's say React has time to commit these changes to the screen. And of course, these changes being our typing into the input field. Now one of React's core principles is consistency. React always updates the DOM in one go, it doesn't show partial results, right? The work in progress tree serves as a draft that's not yet visible to the user, so that React can process all components first and then flush the changes to the screen. It's important to understand that the work during this first phase, the reconciliation phase, can be performed asynchronously. React can process one or more Fiber nodes depending on the available time, then stop to stash the work done and yield to some higher priority event if there is any. It then continues from where it had left off. But sometimes though, it may need to discard the work done and start from the top again. These pauses are made possible by the fact that the work performed during this phase doesn't lead to any user visible changes like DOM updates. In contrast, the second phase, the commit phase, is always synchronous.

9. Commit Phase and Fiber Benefits

Short description:

The commit phase is where React iterates through the effect list and commits the changes to the DOM. After this phase, the work in progress tree has an up-to-date version of the app's state. The use deferred value hook addresses the problems of frame drops and different task priorities. However, concurrent mode features are not yet stable and should not be used in production. Fiber improves React apps by breaking work into small units called Fibers, allowing for pausing and resuming. For more information, refer to the React official docs and explore the React code.

And of course, that's because the work performed during this phase does lead to changes visible to the user, like DOM updates, and that's why React has to do it in a single pass. We cannot pause, show partial results and resume it. It has to go in a single pass.

So, the commit phase. Activities like mutating the DOM or calling lifecycle methods are considered effects and they are stored in the list called the effect list. In our example, the properties of both input and app change. So, their corresponding effects are stored here in this list. And React essentially and the commit phase would iterate this list and commit the changes to the DOM. So, if you remember the input field, we need to change it because we're showing something in that input field. But then also, the app state, we need to change its state as well. So, both of these components, those elements, have effects waiting to be committed to the DOM.

This also means, after all this work is done, the work in progress tree has a more up-to-date version of the state of the app, right? So, React also swaps the root pointers of the current tree and the work in progress tree, thereby effectively swapping the current tree with a draft tree it built up based on the state update we had.

So, to conclude, if you remember, at the beginning of the presentation, I formulated two problems that we have to solve in order to get more responsive user interfaces. The first one was that long-running tasks cause frame drops. And the second one was that different tasks have different priorities. We address these by using the use deferred value hook, which takes advantage of the fiber algorithm and essentially, it assigned a different priority for each task and solved our frame dropping issue we had at the beginning of the presentation. It's also again, important to mention that these concurrent mode features are not yet available in the stable release. And in order to use them, you'd have to install an experimental build of React, which is of course, not recommended to use in production since there are still some bugs and missing features.

So, Fiber will make React apps better in the near term by allowing higher priority updates to jump in front of low priority updates. And it does this by breaking up the work into small units of work called Fibers, which could be paused and resumed later. There's a lot more to talk about and mention when it comes to how Fiber works. So if you are interested in Fiber and all the experimental features of concurrent mode, I would highly encourage you to read more about it. The React official docs are a great resource as well as looking into the React code itself. I hope this presentation gave you a good idea of what's possible with Fiber and got you a little bit excited for the future. I personally think that the combination of Fiber and concurrent mode delivers a very bright future for React, where we as developers could achieve things we couldn't have before and by that enable a better user experience in our applications.

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

React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
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
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
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!
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
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.
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.
React Advanced Conference 2021React Advanced Conference 2021
27 min
(Easier) Interactive Data Visualization in React
If you’re building a dashboard, analytics platform, or any web app where you need to give your users insight into their data, you need beautiful, custom, interactive data visualizations in your React app. But building visualizations hand with a low-level library like D3 can be a huge headache, involving lots of wheel-reinventing. In this talk, we’ll see how data viz development can get so much easier thanks to tools like Plot, a high-level dataviz library for quick & easy charting, and Observable, a reactive dataviz prototyping environment, both from the creator of D3. Through live coding examples we’ll explore how React refs let us delegate DOM manipulation for our data visualizations, and how Observable’s embedding functionality lets us easily repurpose community-built visualizations for our own data & use cases. By the end of this talk we’ll know how to get a beautiful, customized, interactive data visualization into our apps with a fraction of the time & effort!

Workshops on related topic

React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Featured WorkshopFree
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 🤐)
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Featured WorkshopFree
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 Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Featured Workshop
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 Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Featured WorkshopFree
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.
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Featured WorkshopFree
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.
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Featured Workshop
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