How to Reduce React App Loading Time By 70%
Tags: react, javascript, webdev, beginners
Published: 2021-09-14 14:30:59

Steps to decrease your React app initial loading time using code splitting.

We build large-scale apps using React. When building these apps, the major issue we face is app performance. When the app grows larger and larger, the performance might deteriorate. Particularly the initial loading time of the app will be affected more. Initial app loading needs to be fast without showing a blank screen for few seconds to the user. As taking more time to load will create a bad impression for the user.

The major cause for this issue is adding too many components into a single bundle file, so the loading of that bundle file might take more time. To avoid this kind of issue, we need to structure our components in an optimized way. To solve this react itself has a native solution, which is code-splitting and lazy loading. Which allows splitting bundle files into a smaller size.

The best place to introduce code splitting is in routes. Route-based code splitting solve half of the issues. But most of the apps are utilizing only 50% of the advantages of code splitting.

Are we structuring the components properly when using code splitting? We can see why and how to fix it using some code samples. For that, we are going to use a sample React app with some UI components.

In the below screenshot, we can see a dashboard component, which has multiple tabs. Each tab has multiple components.

![dashboard component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m86yxfplki8f6m4y85ps.png)

The Dashboard component uses route-based code splitting as the below code.

{% gist
https://gist.github.com/Nilanth/afd17cb0cb15b014efb99048f44553b0
%}

The Dashboard component contains some sub-components like Sales, Profit, Chart, Tiles and Trends like the below code

{% gist
https://gist.github.com/Nilanth/5ed60b31397d81103e1252d71230d396
%}

We have split the code into routes. so when the app is bundled, we get a separate build file for each route as below

![build-files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilf5k7rggx1l1zv2zccb.png)

From the above image, the file with a size **405.1 KB** is the dashboard component and other files are for the Header, sidebar, other components and CSS.

I have hosted the app in [Netlify](https://www.netlify.com/) to test the performance. As if we test the app locally we cannot find the difference. When I tested the hosted app with [GTmetrix](https://gtmetrix.com/), the dashboard screen took **2.9 seconds** to load, Check the below image for frame by frame loading.

![frames](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfgai6a0xk1w7gfbvz10.png)

The dashboard component is the initial page for this app, so when we hit the App URL **405.1KB** file will be loaded along with the header and sidebar.

Initially, the User will view only the **Sales** tab, But our sample app dashboard component has multiple tabs. So the browser is downloading other tabs code also, it is delaying the first paint for the user. To decrease the initial load time, we need to make some changes to the dashboard component as below

{% gist
https://gist.github.com/Nilanth/f4236862a5748873bc9e53fec9689239
%}

Here I have imported each tab component with lazy loading and wrapped the component with suspense. 

> Here I have added multiple suspense for better understanding, but you can use single suspense for all the components.

I have not done any changes to route level code-splitting. When we build the app, some extra files are added as we have lazy-loaded each tab in the dashboard component. Check the below image for build file separation.

![build-splitcoding](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/268n08iakvetd2hd0b5k.png)

Now let's test the app with GTmetrix again with the above changes. See the App performance in the below image

![frames-code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hi2z7p6rtx8gp1h31fox.png)

As you can see, Now our dashboard component is loaded in **1 second**, as **Sales** tab code only loaded now. We have reduced almost **2 seconds** by making some changes. Let see the comparison of route-based and route, component-based code-splitting in the below images.

![route-based-frames](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/refnafbfm38huv20sajl.png)
<figcaption>Route Based Code Splitting</figcaption>

![component-based-frames](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2agdjoxwnu2h98543s6.png)
<figcaption>Route and Component Based Code Splitting</figcaption>


As you see, this is a huge improvement in the app initial load. Now we have reduced the React app initial load time by 70% with a few tweaks by using code splitting effectively in the dashboard component.

## Reference

1. [Code Splitting](https://reactjs.org/docs/code-splitting.html#code-splitting)
2. [First Contentful Paint](https://gtmetrix.com/blog/first-contentful-paint-explained/)

## Conclusion

Structuring components in an optimized way and using React APIs effectively will increase the performance of large-scale apps.

Thank you for reading.

Get more updates on [Twitter](https://twitter.com/Nilanth).

**You can support me [by buying me a coffee](https://buymeacoffee.com/nilanth) ☕**

## eBook

[Debugging ReactJS Issues with ChatGPT: 50 Essential Tips and Examples](https://nilanth.gumroad.com/l/ztehh)

[ReactJS Optimization Techniques and Development Resources](https://nilanth.gumroad.com/l/NYkdN)

## More Blogs
1. [Twitter Followers Tracker using Next.js, NextAuth and TailwindCSS](https://dev.to/nilanth/twitter-followers-tracker-using-nextjs-nextauth-and-tailwindcss-1em7)
2. [Don't Optimize Your React App, Use Preact Instead
](https://dev.to/nilanth/dont-optimize-your-react-app-use-preact-instead-30og)
3. [Build a Portfolio Using Next.js, Tailwind, and Vercel with Dark Mode Support](https://dev.to/nilanth/build-a-portfolio-using-next-js-tailwind-and-vercel-4dd8)
4. [No More ../../../ Import in React](https://dev.to/nilanth/no-more-import-in-react-2mbo)
5. [10 React Packages with 1K UI Components
](https://dev.to/nilanth/10-react-packages-with-1k-ui-components-2bf3)
6. [Redux Toolkit - The Standard Way to Write Redux](https://dev.to/nilanth/redux-toolkit-the-standard-way-to-write-redux-2g32)
7. [5 Packages to Optimize and Speed Up Your React App During Development](https://dev.to/nilanth/5-packages-to-optimize-and-speed-up-your-react-app-during-development-4h5f)
8. [How To Use Axios in an Optimized and Scalable Way With React](https://dev.to/nilanth/how-to-use-axios-in-an-optimized-and-scalable-way-with-react-518n)
9. [15 Custom Hooks to Make your React Component Lightweight](https://dev.to/nilanth/15-custom-hooks-to-make-your-react-component-lightweight-17cd)
10. [10 Ways to Host Your React App For Free](https://dev.to/nilanth/10-ways-to-host-your-react-app-for-free-27ga)
11. [How to Secure JWT in a Single-Page Application](https://dev.to/nilanth/how-to-secure-jwt-in-a-single-page-application-cko)