Recently open-sourced by Facebook, JavaScript Engine Hermes aims to improve the performance of React Native Apps on Android devices, especially focusing on startup performance, download size, and memory consumption. Facebook claims are backed by initial benchmarking on Microsoft Office and Mattermost, an open source alternative to Slack.
Hermes was born out of Facebook's effort to improve their own mobile apps performance. One of the key factors Facebook engineers identified was the JavaScript engine itself, so they decided to optimize it in the constrained environment of mobile phone apps.
After exploring other options, we built a new JavaScript engine we call Hermes. It is designed to improve app performance, focusing on our React Native apps, even on mass-market devices with limited memory, slow storage, and reduced computing power.
As mentioned, Hermes focuses on three metrics: the time an app requires to become usable (time to first interaction, TTI), app download size (APK size), and memory utilization. Facebook described three major architectural decisions they made to improve those metrics: precompiling the JavaScript source code, ditching the JIT compiler, and adapting garbage collection to mobile OSes constraints.
Precompiling JavaScript into bytecode brings two key benefits to performance. On the one hand, moving ahead parsing and compilation directly reduces the time spent for those tasks at launch time. On the other hand, since the precompilation phase has more relaxed time constraints, generated bytecode is usually smaller and more efficient and the compiler can apply whole-program optimizations such as function deduplication and string table packing. On the bytecode-format front, Hermes bytecode can be mapped into memory without requiring to read the whole file in advance, which brings a significant improvement with slow flash memories and reduces the change of an app being killed by the OS due to excessive memory usage.
(Image from Facebook blog)
Additionally, Facebook maintains, a JIT compiler is optimized for CPU-intensive workloads that are not typical of mobile apps. Thus, not using one does not exact a significant toll while shrinking warm-up time along with native code size and memory consumption.
As a final performance optimization for mobile apps, Hermes uses a garbage collection strategy aimed to minimize the chance that an application is killed by a mobile OS due to its memory usage. Hermes garbage collector thus attempts to reduce the overall virtual memory consumption by implementing on-demand allocation in non-contiguous chunks that can be moved around and returned to the OS when no longer needed.
As mentioned, initial benchmarks done independently by Facebook and Microsoft show significant gains across all three metrics. In particular, Slack-alternative Mattermost halved its time to interaction and application size, with overall memory utilization down by 25%. Microsoft Office also improved its performance in the 20-25% range across all metrics.
(Image from Facebook blog)
Facebook is not targeting its new JavaScript engine as a general replacement to V8, which they still deem to be the most appropriate for Node and browser-based apps. It is worth noting that iOS apps are bound to use exclusively the JavaScript engine included in the iOS SDK, so they cannot benefit from Hermes optimizations.