blog content
The following article is a part of The Ultimate Guide to React Native Optimization and describes how to optimize your Android application startup time with Hermes.
Why is it important?
As you know, well-optimized performance and running at the maximum possible speed are some of the most important factors contributing to the success of mobile applications. They greatly influence how your app is perceived by users and may significantly impact its' ROI. What's more, well-performing apps achieve higher metrics in Google Play Store, which raises the ranking in in-store searches. In this article, we will show you how to achieve all these benefits with the help of Hermes.
In other blog posts based on The Ultimate Guide to React Native Optimization, we touch on the following performance-related topics:
- Optimizing your Android application’s size with Gradle Settings
- Automating your dependency management with `Autolinking`
- Debugging faster and better with Flipper
- Why is it essential to always run the latest version of React Native?
- Experimenting with the New Architecture of React Native
Be sure to check them out. Now let's jump into the main topic.
How does startup time matter influence the success of your app?
Users expect applications to be responsive and to load fast. However, when you’re loading a lot of Android packages during the startup time (which is unnecessary) and using an engine that is not optimized for Android, you’re unlikely to meet these expectations. In such a scenario, your application can end up receiving bad ratings in the App Store or Play Store. In the most extreme situations, it can even get abandoned in favor of the competition.
It is not easy to describe the startup time with a single metric because there are many different stages of the loading phase that can affect how “fast” or “slow” the app feels. For example, in the Lighthouse report, there are as many as six performance metrics used to profile your web application. One of them is Time to Interactive (TTI), which measures the time until the application is ready for the first interaction.
The loading process step by step
There are quite a few things that happen from the moment you press the application icon from the drawer for the first time.
The loading process starts with a native initialization (1), which loads the JavaScript VM and initializes all the native modules. It then continues to read the JavaScript from the disk (2), loads it into the memory, parses and starts executing. In the next step (3), React Native starts loading React components and sends the final set of instructions to the UIManager. Finally, the UIManager processes the information received from the JavaScript and starts executing native instructions (4) that will result in the final native interface.
As you can see in the diagram above, there are two groups of operations that influence the overall startup time of your application.
The first one involves operations 1 and 2 from the diagram and describes the time needed for React Native to bootstrap (to spin up the VM and for the VM to execute the JavaScript code). The other one includes the remaining operations 3 and 4 and is associated with the business logic that you have created for your application. The length of this group is highly dependent on the number of components and the overall complexity of your application.
This article focuses on the first group – the improvements related to your configuration and not the business logic itself. If you have not measured the overall startup time of your application or have not played around with things such as Hermes yet - keep on reading.
Long startup times and slow UX on Android can be one of the reasons your app gets bad rating and ends up being abandoned.
Creating applications that are fun to play with is extremely important, especially considering how saturated the mobile market already is. Now, all mobile apps have to be not only easy to understand and intuitive, but also pleasant to interact with.
There is a common misconception that React Native applications come with a performance trade-off compared to their native counterparts. The truth is that with enough attention and configuration tweaks, they can load just as fast and without any considerable difference.
How to improve your app’s performance with Hermes
While React Native application takes care of a native interface, it still requires JavaScript logic to be running at a runtime. To do so, it spins off its own JavaScript virtual machine. By default, it uses JavaScriptCore. This engine is a part of WebKit and by default is only available on iOS. Now it’s also a preferred choice for compatibility purposes on Android. It’s because using the V8 engine (that ships with Chrome) could potentially increase the differences between Android and iOS, and make sharing the code between the platforms way more difficult.
JavaScript engines don’t have an easy life. They constantly ship new heuristics to improve the overall performance, including the time needed to load the code and then to execute it. To do so, they benchmark common JavaScript operations and challenge the CPU and memory needed to complete this process.
Note: The V8 team has recently published a blog post on improving the regular expressions’ performance. Be sure to check it out.
Most of the work of developers handling the JavaScript engines is being tested against major and most popular websites, such as Facebook or Twitter. It is not a surprise that React Native uses JavaScript in a different way. For example, the JavaScript engine made for the web doesn’t have to worry much about the startup time. The browser will be most likely already running at the time of loading a page. Because of that, the engine can shift its attention to the overall CPU and memory consumption, as web applications can perform a lot of complex operations and computations, including 3D graphics.
As you could see in the performance diagram presented above, a JavaScript virtual machine consumes a big chunk of the app’s total loading time. Unfortunately, there is little you can do about it unless you build your own engine. That’s what the Facebook team ended up doing.
Meet Hermes - a JavaScript engine made specifically with React Native in mind. It is optimized for mobile and focuses on relatively CPU-insensitive metrics, such as application size and memory consumption. Chances are you’re already using it! As of v0.70, React Native has been shipping with Hermes turned on by default, which marks an important milestone in the engine’s stability.
It’s come a long way from the bare-bones Android-only engine open-sourced in 2019, with a carefully curated set of supported JS features—due to size constraints—through finding low-size-footprint ways of adding more EcmaScript spec features, like Proxies and Intl, until making it available for macOS and iOS. Today Hermes is still small enough (~2 MB) to provide significant improvements to apps’ TTI and gives us a set of features rich enough to be used in most of the apps out there.
Before we go into the details of enabling Hermes in existing React Native applications, let’s take a look at some of its key architectural decisions.
Bytecode precompilation in Hermes reduces time to execute business logic
Typically, the traditional JavaScript VM works by parsing the JavaScript source code during the runtime and then producing the bytecode. As a result, the execution of the code is delayed until the parsing completes. It is not the same with Hermes. To reduce the time needed for the engine to execute the business logic, it generates the bytecode during the build time.
It can spend more time optimizing the bundle using various techniques to make it smaller and more efficient. For example, the generated bytecode is designed in a way so that it can be mapped in the memory without the eager loading of the entire file. Optimizing that process brings significant TTI improvements as I/O operations on mobile devices tend to increase the overall latency.
No JIT in Hermes translates into increased TTI
The majority of modern browser engines use just-in-time (JIT) compilers. It means that the code is translated and executed line-by-line. However, JIT compiler keeps track of warm code segments (the ones that appear a few times) and hot code segments (the ones that run many times). These frequently occurring code segments are then sent to a compiler that, depending on how many times they appear in the program, compiles them to the machine code and, optionally, performs some optimizations.
Hermes, unlike the other engines, is an AOT(ahead-of-time) engine. It means that the entire bundle is compiled to bytecode ahead of time. As a result, certain optimizations that JIT compilers would perform on hot code segments are not present.
On the one hand, it makes the Hermes bundles underperform in benchmarks that are CPU-oriented. However, these benchmarks are not really comparable to a real-life mobile app experience, where TTI and application size takes priority. On the other hand, JIT engines decrease the TTI as they need time to parse the bundle and execute it in time. They also need time to “warm up”. Namely, they have to run the code a couple of times to detect the common patterns and begin to optimize them.
How to start using Hermes on Android and iOS
If you want to start use Hermes, make sure make sure to turn the <rte-code>enableHermes<rte-code> flag in <rte-code>android/app/build.gradle<rte-code> to <rte-code>true<rte-code>:
For iOS, turn the <rte-code>hermes_enabled<rte-code> flag to <rte-code>true<rte-code> in <rte-code>ios/Podfile<rte-code>:
In both cases, whenever you switch the Hermes flag, make sure to rebuild the project according to instructions provided in the native files. Once your project is rebuilt, you can enjoy a faster app boot time and likely a smaller app size.
Note: You’ll find more insights on compiling Hermes and integrating it with React Native on our blog. If you prefer to watch or listen, we also have an entire episode of The React Native Show podcast dedicated to Hermes, so be sure to check it out.
Improving startup time with Hermes leads to better performance and UX
Making your application load fast is not an easy task. It’s an ongoing effort and its final result will depend on many factors. You can control some of them by tweaking both your application’s configuration and the tools it uses to compile the source code.
Turning Hermes on is one of the things you can do today to improve your application’s performance drastically. Apart from that, you can also look into other significant improvements shipped by the Facebook team. To do so, get familiar with their write-up on React Native performance. It is often a game of tiny and simple improvements that make all the difference when applied at once. The React Native core team has created a visual report on benchmarking between stock RN and Hermes-enabled RN, you can check it out here.
As we have mentioned in the article on running the latest React Native, Hermes is one of those assets that you can leverage as long as you stay up to date with your React Native version. Doing so will help your application stay on top of the performance game and let it run at maximum speed.
Need help with performance? Give us a shout!
If you’re struggling with improving your app performance, don’t hesitate contact us. We’re the official Meta and Vercel partners, and we’ve been delivering high-quality solutions for our clients and contributing greatly to the React Native ecosystem for a while. If you’re looking for a performance optimization partner, our React Native app development company is surely the right one.