I’ve worked for a year optimizing one of the largest banks mobile apps performance. We ended up getting it from 14s TTI to around 2.5s on a slow Huawei P20 Lite. Here is what i wish i knew before starting!
0. Find Your Monsters
A common mistake of optimizing is focusing on everything all at once. Get your hands on a slow android phone, install your app and do a complete test run and identify your highway functionality — Ask your self what does your users do most? which paths is performance a crucial part of the experience?
- Try removing all animations — how does your app run?
- Try rendering components with data not being fetched — is it still slow?
- Try removing a component all together — is the rendering time significantly improving?
Figure out the exact way things are rendered, when is data fetched, when is a new component rendered into the tree — Once you know what is causing the slow downs in your app and it will be much easier to fix.
1. Measure, Measure and Measure Again
Every time you do an optimization, test and measure your change — how do you do that? Use a stop watch, use react-native-debugger or whole third other tool — there’s no silver bullet when it comes to react native performance, cases vary a lot so measuring before and after reliably is absolutely crucial.
I’ve written a longer article about this earlier if your interested 🎉
2. Visualize your bundle
react-native-bundle-visualizer is a great tool for investigating your bundle size. I’ve had a lot of ‘aha’-moments using as i finally had the full picture of the app. As the name implies this will build your app-bundle, help you visualize exactly how big your bundle is and how its size spread on different components and node modules. Is there something here that stands out? Do you really need moment taking up 6% of your apps bundle size?
3. Load Only What You Need When You Need It
This is one of the biggest improvements you can do to start up time. It’s one of those things that are a lot more complex than it should be in react native but its definitely worth it. On my old project our startup time went down from 14 to 4 seconds on the slowest android device we could find.
Facebooks own react native post on performance is one of the best resources on this.
Delay anything that isn’t crucial for the user
You can see this effect very much on apps like facebook or instagram, they load the bare minimum, then progressively load more and more. This means data fetching and heavy components, the devices are incredibly fast out there, perceived performance is all about loading the most important stuff first. Here’s a solid guide for it.
4. Optimize and standardize assets
Optimizing assets in your React Native apps provides an easy way to achieve big performance gains with little effort.
Why? PNG’s definitely aren’t created equal, especially if you’re working with multiple designers who use various export settings when creating assets.
You can use software like ImageOptim — or automate the whole process doing something like this.
5. Embrace perceived performance
6. Utilize as much open source as possible and keep them up to date
One of the bitter sweet things i realized developing at a major IT-company is that we could almost never outperform the big open source projects out there. There are thousands of contributors and their range of devices and knowledge between the developers are often far greater than what your company or team has to offer.
The focus of the React Native community has been on app performance in general the last 2 years, especially the react native core team has made some big steps in 0.59 (New JSC) and 0.60.2 (Hermes introduced!).
7. Rewrite Your Animations
Nothing screams “slow app” like laggy animations to the user. Animations can also be a huge performance hog on the JS-thread, rewriting these to use the native thread instead can make a huge difference. Almost every animation is possible using the limited subset of Animated with useNativeDriver:true set — its all about being clever with the implementation.
8. Avoid Micro Optimizations Until Later
They’re often not the real culprit. Focus on the bigger picture before rewriting things unnecessary inline functions etc.
9. Have Fun With It!
Optimizing apps are a lot of work and they’re all different in architecture — but getting into performance optimizing is an amazing way to understand the nitty-gritty of how the inside of React really works. Hope you enjoyed the article! 🎉
Further reading and watching
I absolutely recommend watching Konstantin Raev talk about performance in facebooks marketplace, it’s very pragmatic and can easily be translated into real world situations. The react native teams official guide on performance is solid and definitely worth a read.