Optimizing Performance in ReactJS
Last Updated :
11 Mar, 2022
Performance matters a lot in any application. In ReactJS, Optimizing the performance is an important task to do before launching a React application. There are different ways to explore optimizing a React application that can help to increase speed and efficiency as well as user experience.
ReactJS is a JavaScript library for making UI and apps. React provide different ways to minimize the costly DOM operations required to update the UI. For most of the time, using React will lead to a fast UI experience without doing much work to optimize for performance.
There are several ways you can speed up your React application. These are the methods to follow:
- Using React.Fragment - When working with React, there are cases where we need to render multiple elements or return a group of related items. Using additional div to wrap will fix this, but it comes with a problem. Because we are adding an extra node to the DOM, which is totally not useful. In cases like this, where we have the child component which is enclosed within a parent component. Here we encounter this problem. Let’s solve this by using React Fragment, which will help not to add any additional node to the DOM.
- Use of production build - Another way of optimizing a React app is by making sure you bundle your app for production before deploying. By default, the app is in development mode, which means React will include helpful warnings which come with it. This method is useful while you are developing a ReactJS application, but it creates a problem that your app size is large and responses are slower than usual. If the project is built with create-react-app, This can be fixed by running npm run build before the deployment, which will create a production-ready build of your app in a build/ folder that can be then deployed. This can make sure that your app is in either development or production mode using the React Developer Tools.
From the project directory run the code:
npm run build
This will create a production build of the ReactJS app.
- Prefer component state local - Whenever a state updates in a parent component, it re-renders the parent and its child components. Therefore, we need to make sure that re-rendering a component only happens when it is necessary. We can achieve this by making it local to that part of the code.
- Memoizing React components - React.memo is a great way of optimizing performance as it helps cache functional components.
- Memoization in general is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. Whenever a function is rendered using this technique, it saves the output in memory, and the next time the function with the same arguments is called it returns the saved output without executing the function again.
- Windowing or list virtualization - Windowing or List virtualization is a concept of only rendering or writing the visible portion in the current “ window ” to the DOM. The number of items rendered for the first time is smaller than the original one. The items which are not rendered in the first small window will load when scrolling down to it. The DOM nodes of items that exit from the current window are replaced by the new which were not rendered the first time. This improves the performance of rendering a large list.
Let's create an application and implement all the above to optimize our react app
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app example
Step 2: After creating your project folder i.e. example, move to it using the following command:
cd example
Project structure: It will look like this.
project structure
Example: To demonstrate to optimize react app. Write down the following code in index.js and App.js
JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
JavaScript
import React from "react";
const numbers = [1, 2, 3, 4, 5];
function List(props) {
const numbers = props.numbers;
const listItems = numbers.map((number, index) =>
<li>{number}</li>);
return (
<> {/*This is a short format of fragment */}
<div>List of numbers</div>
<ul>{listItems}</ul>
</>
);
}
function App() {
return (
<React.Fragment>
{/*Fragment is a component that is not rendered */}
<List numbers={numbers} />
</React.Fragment>
);
}
export default App;
In the above code, we used the React fragment, kept components local, and optimized the React app.
Step to run the application: Run the following command from the root directory of the project.
npm start
Output :
Output
Similar Reads
Performance Hooks in React While developing React Applications, optimizing performance is one of the most important things for delivering a seamless user experience. One common way to boost performance is to minimize unnecessary re-renders by skipping repetitive calculations. React provides two powerful hooks, useMemo and use
3 min read
How memoization optimizes performance in React ? Have you ever wondered how computers can do complex tasks super fast? Well, memoization is like a secret trick that makes certain tasks much quicker by remembering past results instead of recalculating them every time. It's a game-changer for functions that do the same calculations over and over aga
6 min read
How to optimize the performance of React app ? The operations involved in keeping the DOM updates are costly but react uses several techniques to minimize the no. of operations which leads to natively faster UI for many cases. The following techniques can be used to speed up the application: Table of Content Use binding functions in constructors
3 min read
Optimizing Your MERN Stack Application Performance The MERN stack, comprising MongoDB, Express.js, React.js, and Node.js, is a popular choice for developing modern web applications. However, like any technology stack, optimizing performance is crucial to ensure a responsive and efficient user experience. This article delves into strategies and best
3 min read
Mastering Performance Optimization Techniques with React Hooks In this article, we will explore advanced performance optimization techniques using React Hooks. We'll delve into memoization, callback optimization, preventing unnecessary renders, state updates, and more. By mastering these techniques, developers can significantly enhance the performance of their
6 min read