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 to implement Conditional Rendering in React?
Next article icon

How to improve slow React application rendering ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Slow React application rendering as its name suggests is a small or large delay in rendering elements. There may be multiple reasons for that. React uses the concept of Virtual DOM. All the elements in React are rendered using the render method in which we have to provide an id of an element (mostly div) to be rendered. Every element in React is immutable. Hence, to Update the element, we have to call the Render method again.

Prerequisites:

  • React JS
  • React JS Rendering Element
  • React JS virtual DOM
  • React JS useMemo hook

Approach to Improve Slow React Application Rendering:

Slow React Application Rendering may occur due to heavy components, images, bad code practices unnecessary renders due to changes in states. This problem can be reduced by code splitting, avoiding unnecessary re-renders and memoization. We will be using useMemo hook to reduce calculations and updates to improve rendering.

Steps to create React Application

Step 1: Create a React application using the following command:

npx create-react-app useMemo 

Step 2: After creating your project folder i.e. useMemo, move to it using the following command:

cd useMemo

Project Structure:

Example 1: This example uses useState hook to store and update state and render on UI.

JavaScript
// Filename - App.js  import { useState } from "react"; import "./App.css";  export default function App() {     // 1st counter state     const [counter1, setCounter1] = useState(0);      // 2nd counter state     const [counter2, setCounter2] = useState(0);      // Sample Heavy Calculation Function     const heavyCalculation = () => {         let i = 0;         for (let outer = 0; outer < 10000; outer++) {             for (let temp = 0; temp < 10000; temp++) {                 while (i < 10000) i++;             }         }         return counter1 % 2 === 0 ? true : false;     };      return (         <div className="App">             {heavyCalculation()                 ? `Counter is even with value ${counter1}`                 : `Counter is odd with value ${counter1}`}             <br />             <button                 onClick={() => {                     setCounter1(counter1 + 1);                 }}             >                 Increment counter1             </button>             <br />             Counter 2 is {counter2}             <br />             <button                 onClick={() => {                     setCounter2(counter2 + 1);                 }}             >                 Increment counter2             </button>         </div>     ); } 

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Here, in the output, a heavy function is being called on Rendering. Now even though it is being used for the first counter, but it still causes render due to counter2 slow.


Example 2: This example implements useMemo Hook to avoid unnecessary update and hence reduce rerenders on the UI.

App.js
import { useState, useMemo } from "react"; import "./App.css";  export default function App() {    // 1st counter state   const [counter1, setCounter1] = useState(0);    // 2nd counter state   const [counter2, setCounter2] = useState(0);    // Our custom useMemo Function   const useMemoFunction = useMemo(() => {     let i = 0;     for (let outer = 0; outer < 10000; outer++) {       for (let temp = 0; temp < 10000; temp++) {         while (i < 10000) i++;       }     }     return counter1 % 2 === 0 ? true : false;   }, [counter1]);    return (     <div className="App">       {useMemoFunction         ? `Counter is even with value ${counter1}`         : `Counter is odd with value ${counter1}`}       <br />       <button         onClick={() => {           setCounter1(counter1 + 1);         }}       >         Increment counter1       </button>       <br />       Counter 2 is {counter2}       <br />       <button         onClick={() => {           setCounter2(counter2 + 1);         }}       >         Increment counter2       </button>     </div>   ); } 

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Here, heavy functionality is used by useMemo hook containing counter1 as a dependency. Due to this Counter1 still shows delay, but it does not affect the performance of counter2.



Next Article
How to implement Conditional Rendering in React?
author
namancourses
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Questions

Similar Reads

  • How to implement Conditional Rendering in React?
    This article explores JavaScript's conditional rendering in React JS, including various methods, syntax, and applications, leveraging React's virtual DOM, reusable components, props, and rapid rendering capabilities in single-page UI applications. We will use the following approaches to implement co
    4 min read
  • How to Optimize the Performance of React-Redux Applications?
    Optimizing the performance of React-Redux applications involves several strategies to improve loading speed, reduce unnecessary re-renders, and enhance user experience. In this article, we implement some optimization techniques, that can significantly improve the performance of applications. We will
    9 min read
  • Movie Web Application with ReactJS
    React Movie APP is a movie web application project that involves creating components to display a list of movies and details about each movie along with a search functionality. Preview Image of Final Output: Approach for React Movie AppTo create the movie web application in React we will be using an
    7 min read
  • How to Implement Caching in React Redux applications ?
    Caching is the practice of storing frequently used or calculated data temporarily in memory or disk. Its main purpose is to speed up access and retrieval by minimizing the need to repeatedly fetch or compute the same information. By doing so, caching helps reduce latency, conserve resources, and ult
    3 min read
  • How to call function inside render in ReactJS ?
    In React JS, the render method is a fundamental part of a component's lifecycle. It is responsible for rendering the component's JSX markup onto the DOM. While the render method itself should primarily handle rendering JSX, there are scenarios where you may need to call a function inside the render
    3 min read
  • How to optimize React component to render it ?
    ReactJS mainly depends upon the props (which are passed to it) and the state of the Component. Hence to reduce the number of times Component renders we can reduce the props and state it depends upon. This can be easily done by separating the logic of one component into several individual child compo
    3 min read
  • How can you optimize the performance of React-Redux applications?
    Performance optimization in React-Redux involves improving the speed, efficiency, and responsiveness of applications by minimizing rendering times, reducing unnecessary re-renders, and optimizing data fetching and processing. By implementing optimization techniques, you can enhance the overall user
    5 min read
  • How to Solve Too Many re-renders Error in ReactJS?
    "Too many re-renderers" is a React error that happens after you have reached an infinite render loop, typically caused by code that, in a useEffect hook or the main body of the component itself, unconditionally calls state setters. PrerequisitesNPM & NodeJSReactJSReactJS lifecycleReactJS HooksUn
    5 min read
  • Explain the purpose of render() in ReactJS
    Render in React JS is a fundamental part of class components. It is used to display the component on the UI returned as HTML or JSX components. The ReactDOM.render() function takes two arguments, HTML code and an HTML element. Purpose of render()React renders HTML to the web page by using a function
    2 min read
  • How to prevent a component from rendering ?
    In React JS, preventing a component from rendering simplifies to conditional rendering of the component. When UI is designed using React, we come across a situation when components are to be rendered on the screen based on some condition. For eg, In a University Information System, when a teacher lo
    3 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