7 Ways to Optimize Your React App for Speed
Make your React apps faster with these tips
Introduction
React apps are fast. But what if you could make them faster? The complexity increases as you add more components, and render times can also increase. There’s no need to fear, though! With some extra thought during design and development, you can increase the speed of your app without sacrificing functionality or adding bloat to your codebase. Follow this blog post to see seven ways you can speed up your React app today!
Don't Overcomplicate it
Don't overcomplicate your app by adding too many features. If you're going to add a new feature, it should be really useful and not just a novelty. It's better to have one or two key features that are well-executed than a dozen half-baked ones.
Tips for Faster Web Apps - Deduplicate! Keep in mind that every unique object on the page is another object in the browser's memory. So if you can get rid of any duplicate code, do so! - Create smaller components: Rather than creating larger components with more functionality (like the navbar) try making multiple smaller components instead.
Don’t Include Unnecessary Modules
If you're using a module from npm
, you must know how much of your bundle size it takes up. One way to do this is by installing an app such as Webpack Bundle Analyzer, which gives you a tree view of your bundle and the size of each module.
Use Lazy Loading Techniques
Here are a few techniques you can use to make your react app faster:
- Use Lazy Loading Techniques. This is the easiest way and has the highest impact on performance. It involves loading data as needed when it becomes available, rather than loading all of the data at once. For example, if you have a list of items that only show up on hover, there's no need to load those items until they've hovered over. You could also delay rendering some DOM elements until after their children have been loaded and rendered.
- Make Sure You Aren't Loading Code You Don't Need. You should be using ES6 imports or Webpack aliases to avoid bundling code with dependencies it doesn't need. If an import isn't used in any file in the bundle, it will never get executed even if its dependencies were included earlier in the bundle. Remember to always use
import './foo';
instead of just including./foo
. - Another strategy would be to create two separate bundles - one for shared modules and one for user-specific modules.
Put Components on Separate Layers
React uses a Virtual DOM that calculates the difference between what the user sees and what's actually on the page. If there is a change, React will make an update for that part of the page. This can cause some performance issues because it has to recalculate everything every time something changes.
To get around this, you can put each component on its layer so they don't affect each other when they are changed. Try using a library like react-virtualized
or Radium
. You could also try creating multiple components and use one as the parent of the other to keep them together or set different heights or widths on them if they have overlapping content.
Avoid Stateless Functional Components (If Possible)
Stateless Functional Components are a great way to isolate the logic of your component without the overhead of maintaining a state. However, they're not always necessary and can slow down your app if you're using them in areas where they don't need to be.
- Avoid Stateless Functional Components (If Possible)
- If You Do Use Stateless Functional Components, Cache Their Results In Props or State
- Don't Render Unnecessary Nodes: It's important to keep only what is necessary on the page when rendering components. If something doesn't need to render on every page then it shouldn't render at all.
- Avoid Unnecessary Updates: We tend to think that updating our UI frequently is a good thing but that might not always be true!
Avoid Deeply Nested Components
One of the easiest ways to optimize your app is by avoiding deeply nested components. When you have a lot of nested components and they're all nested inside each other, the browser will have to do more work than if they were just on one level. This is because it has to calculate how it should render each layer and do so in the correct order. It's always best to keep things as simple as possible. If you find yourself nesting too deep, take a step back and figure out what can be grouped into one component instead.
Store the Data Closest to Where it is Needed
A common rule of thumb is that the data should be stored in memory as close to the code that needs it as possible. This means if the data is needed by a component, then it should be stored in a property on that component. If it is used by multiple components, then one solution may be to store the data in an object and pass it down as props. Once you figure out where your data will live, you want to make sure that storing and retrieving this information takes place quickly. One way to do this is to use persistent immutable data structures like Immutable.js
or Mori
.
A second technique would be to store the cache in fast storage like Local Storage
or IndexedDB
so that lookups happen locally without needing a round-trip over the network.
This is because Local Storage can be accessed quickly from JavaScript without an internet connection. This allows you to store a large cache of data on each user’s computer and index it as needed by referencing its unique ID. Mori supports creating indices in-memory, in-memory + disk, or disk only which provides great flexibility when choosing a storage solution. It also takes care of sorting, so you can avoid extra comparisons if your information naturally sorts itself when returned.
Conclusion
Optimizing your app for speed is important if you want users to stick around. Keep these tips in mind when designing, developing, and deploying your next react app. They'll help make sure it performs well from the get-go!