React Optimization Techniques Part 1
React Optimization Techniques Part 1
React Perfomance
Optimization
Techniques (1)
@oluwakemi Oluwadahunsi
Introduction
React is renowned for its efficiency and flexibility
in building dynamic user interfaces. However, as
applications grow in complexity and size, ensuring
optimal performance becomes important.
@oluwakemi Oluwadahunsi
Understanding React's
Rendering Process
Before diving into optimization techniques, it's
essential to understand how React renders
components:
@oluwakemi Oluwadahunsi
Techniques for Performance
Optimization In React
There are sevral techniques involved in optimizing
performance in React, which are:
@oluwakemi Oluwadahunsi
Function and Component
Memoization
Memoization is a technique in programming that
involves caching the results of expensive function
calls and returning the cached result when the
same inputs occur again.
React.memo( )
useMemo( )
useCallback( )
1. React.memo( )
React.memo() is used to memoize entire functional
components.
@oluwakemi Oluwadahunsi
For example: Memoizing a Child Component
@oluwakemi Oluwadahunsi
In this example,
second arguement
@oluwakemi Oluwadahunsi
When to Use React.memo:
Pure Functional Components: Components
that render the same output given the same
props.
Performance-Critical Components:
Components that are expensive to render or
have frequent re-renders.
Benefits:
Reduced Re-Renders: Prevents unnecessary
updates, saving computational resources.
Improved Performance: Enhances the overall
efficiency of the application, especially with
large component trees.
Considerations:
Shallow Comparison: React.memo performs a
shallow comparison of props. For deep prop
comparisons, consider using a custom
comparison function.
Overuse: Using React.memo indiscriminately
can lead to complexity without significant
performance gains.
@oluwakemi Oluwadahunsi
2. useMemo( )
useMemo( ) is a hook used to memoize values that
result from expensive calculations. It caches the
computed result and only recomputes it when one
of its dependencies changes.
@oluwakemi Oluwadahunsi
In this example:
Without useMemo( ): The ExpensiveCalculation
function would run on every render, even when
number hasn’t changed, leading to inefficient
re-renders.
Benefits:
@oluwakemi Oluwadahunsi
3. useCallback( )
useCallback() is used to memoize callback
functions. It ensures that the same function
reference is maintained across renders, preventing
unnecessary re-renders of child components that
receive the function as a prop.
@oluwakemi Oluwadahunsi
In this example:
Without useCallback(): The Button component
would re-render every time the parent
@oluwakemi Oluwadahunsi
Implementing Virtualization for
Large Lists
In React applications, dealing with large lists or
datasets can pose significant performance
challenges.
@oluwakemi Oluwadahunsi
What is Virtualization in React?
Virtualization (or windowing) is a performance
optimization technique that involves rendering
only the visible part of a list and dynamically
loading the remaining items as the user scrolls.
For Example:
@oluwakemi Oluwadahunsi
Here’s a simple example of how to use react-
window to virtualize a large list of items.
@oluwakemi Oluwadahunsi
In this example:
@oluwakemi Oluwadahunsi
By default, react-window assumes that all items in
the list have a fixed height. If your items have
variable heights, you can use the VariableSizeList
component likw this:
For Example:
@oluwakemi Oluwadahunsi
Here’s an example of how to use react-virtualized
to virtualize a list of items.
@oluwakemi Oluwadahunsi
In this example:
@oluwakemi Oluwadahunsi
Benefits of Virtualization
@oluwakemi Oluwadahunsi
Optimizing Images and Assets
Just like every other web development tools,
another key aspect of performance optimization
technique in React is optimizing images and other
static assets such as CSS, JavaScript files, fonts,
and videos.
@oluwakemi Oluwadahunsi
Best Practices for Image Optimization in React
@oluwakemi Oluwadahunsi
2. Responsive Image Handling: Using srcset and
sizes
For mobile-first performance, images should be
responsive, meaning different image sizes are
served based on the device’s screen size and
resolution.
The <img> tag can take advantage of the srcset and
sizes attributes to serve different image versions.
different images width rendered
for different device screen width
responsive sizes
@oluwakemi Oluwadahunsi
Using this method, images are loaded as the user
scrolls, improving performance by reducing the
number of images loaded initially.
@oluwakemi Oluwadahunsi
Optimizing Other Static Assets in React
@oluwakemi Oluwadahunsi
2. Font Optimization
@oluwakemi Oluwadahunsi
3. Video Optimization
@oluwakemi Oluwadahunsi
Stay Tuned as we
continue the
second part of
React
Performance
Optimization
Tomorrow.
@oluwakemi Oluwadahunsi
I hope you found this material
useful and helpful.
Remember to:
Like
&
Share with your network, be
helpful to someone 👌
@oluwakemi Oluwadahunsi
Hi There!
Thank you for reading through
Did you enjoy this knowledge?
kodemaven-portfolio.vercel.app