React Rendering #2 — Optimization

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this post. Find the quick read 👇

In this React rendering series, we will cover the following
React rendering basics
React rendering optimization
React rendering state
In this React rendering optimization post, we will see the following
Optimization opportunities
Optimization techniques
Reference memoization
Performance measurement
Rendering is React’s way of knowing if it needs to make changes in the DOM, but there are certain cases where work and calculations performed during the render phase can be a wasted effort. After all, if a component’s render output is identical, there will be no DOM updates, thus the work wasn’t necessary.
Render output should always be based on the current combination of props and state, so it is possible to know ahead of time if a component’s render output will be the same so long as its props and state remain unchanged. This is the key observation on top of which optimizing React rendering is based, as it hinges on our code doing less work and skipping component rendering when possible.
React offers a handful of APIs that allow us to optimize the rendering process
shouldComponentUpdate (class components): Lifecycle method, called before rendering, returning a boolean (false to skip rendering, true to proceed as usual). Logic can vary as necessary, but the most common case is checking if the component’s props and state have changed.
React.PureComponent (class components): A base class that implements the previously described props and state change check in its shouldComponentUpdate lifecycle method.
React.memo() (any component): Higher-order component (HOC) that wraps any given component. It implements the same kind of functionality as React.PureComponent, but can also wrap function components.
All of these techniques use shallow equality for comparisons. Skipping rendering a component means skipping the default recursive behavior of rendering children, effectively skipping the whole subtree of components.
Passing new references as **props** to a child, the component doesn’t usually matter, as it will re-render regardless of when the parent changes. However, if you are trying to optimize a child component’s rendering by checking if its props have changed, passing new references will cause a render. This behaviour is ok if the new references are updated data, but if it’s a new reference to the same callback function passed down by the parent, it’s rather problematic.
As far as functional components are concerned, React provides the useMemo hook for memoizing values and the useCallback hook specifically for memoizing callbacks.
useMemo and useCallback can provide performance benefits but, as with any other memoization usage, it’s important to think about their necessity and the net benefit they provide in the long run.
A good rule of thumb is to consider using them for pure functional components that re-render often with the same props and/or might do heavy calculations and avoid them elsewhere.
React Developer Tools provides a handy Profiler tab that allows you to visualize and explore the rendering process of your React applications. Under this tab, you will find a settings icon that will allow you to Highlight updates when components render, as well as Record why each component rendered while profiling

React’s development builds are significantly slower than production builds, so take all the measurements you see with a grain of salt as absolute times in development are not a valuable metric. Identifying unnecessary renders, memoization, and optimization opportunities, as well as potential bottlenecks, is where you should focus.
