Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How memoization optimizes performance in React ?
Next article icon

Optimizing Performance in ReactJS

Last Updated : 11 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 

  • index.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') ); 
  • App.js
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

Next Article
How memoization optimizes performance in React ?
author
kvvik2020
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • React-Questions

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
  • How to Optimize WebGL Performance?
    WebGL (Web Graphics Library) is a powerful JavaScript API used to render 3D and 2D graphics within any compatible web browser. However, achieving optimal performance with WebGL requires careful attention to various aspects of your code and rendering pipeline. Key strategies to optimize WebGL perform
    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
  • Optimizing Performance of List Rendering with useMemo Hook in React
    List rendering is a common task in web development, especially in React applications where components often display dynamic lists of data. However, rendering large lists can lead to performance issues if not handled efficiently. In this article, we'll explore how to optimize the performance of list
    4 min read
  • Optimizing Performance with useMemo and useCallback Hooks
    In React applications, optimizing performance is crucial for ensuring smooth user experiences, especially in components with complex computations or frequent re-renders. Two hooks provided by React, useMemo, and useCallback, offer efficient ways to achieve performance improvements by memoizing value
    4 min read
  • How useMemo Hook optimizes performance in React Component ?
    The useMemo hook in React optimizes performance by memoizing the result of a function and caching it. This caching ensures that expensive computations inside the useMemo callback are only re-executed when the dependencies change, preventing unnecessary recalculations and improving the rendering perf
    2 min read
  • Next.js Bundle Optimization to improve Performance
    In this article, We will learn various ways to improve the performance of the NextJS bundle which results in increasing the performance of NextJS applications in Google PageSpeed Insights or Lighthouse. As per the documentation, NextJS is a React framework that gives you the building blocks to creat
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences