Facebook opened the Hermes JavaScript engine code

Facebook Open Source Lightweight JavaScript Engine Hermes, optimized for running applications based on the framework React native on the Android platform. Hermes support built in in React Native as of today's 0.60.2 release. The project aims to solve problems with long startup times for native JavaScript applications and significant resource consumption. Code is written in C++ and distributed under the MIT license.

Among the advantages of using Hermes, there is a reduction in application launch time, a decrease in memory consumption and a decrease in the size of the application. When using V8, the stages of parsing the source code and compiling it on the fly are the longest. Hermes brings these steps into the build phase and allows applications to be delivered in the form of a compact and efficient bytecode.

For the direct execution of the application, a virtual machine developed within the framework of the project with the SemiSpace garbage collector is used, which allocates blocks only as needed (On-demand), supports the movement and defragmentation of blocks with the release of freed memory to the operating system, without periodically scanning the contents of the entire heap.

JavaScript processing is divided into several stages. Initially, source texts are parsed and an intermediate representation of the code is generated (Hermes IR) based on the representation SSA (Static Single Assignment). Next, the intermediate representation is processed in the optimizer, which applies forward static optimization techniques to transform the primary intermediate code into a more efficient intermediate representation while retaining the original semantics of the program. At the last stage, the bytecode for the register virtual machine is generated.

In the engine supported by part of the ECMAScript 2015 JavaScript standard (the ultimate goal is full support) and ensures compatibility with most existing React Native applications. Hermes decided not to support local eval(), "with" expressions, reflection (Reflect and Proxy), API Intl API and some flags in RegExp. To enable Hermes in a React Native application, just add the "enableHermes: true" option to the project. It is also possible to build Hermes in CLI mode, allowing you to execute arbitrary JavaScript files from the command line. For debugging, a lazy-compilation mode is available, which allows you not to compile JavaScript every time during the development process, but to generate bytecode on the fly already on the device.

At the same time, Facebook does not plan to adapt Hermes for Node.js and other solutions, focusing only on mobile applications (AOT compilation instead of JIT is most optimal in the context of mobile systems, which have limited RAM and slower Flash). Pre-Microsoft Performance Testing has shownthat when using Hermes, the Microsoft Office application for Android becomes available for work in 1.1 seconds. after launch and consumes 21.5MB of RAM, while when using the V8 engine, it takes 1.4 seconds to start, and the memory consumption is 30MB.

Add-on: Facebook ΠΎΠΏΡƒΠ±Π»ΠΈΠΊΠΎΠ²Π°Π» own test results. When using Hermes with the MatterMost app, Time To Interact (TTI) was reduced from 4.30 to 2.01 seconds, APK size was reduced from 41 to 22 MB, and memory consumption was reduced from 185 to 136 MB.

Source: opennet.ru

Add a comment