BinaryAST: a Cloudflare, Mozilla and Facebook project to speed up JavaScript loading

binary-ast

The engineers from Cloudflare, Mozilla, Facebook and Bloomberg have started working on a new project called “binaryAST" to speed up delivery and processing of JavaScript code when opening sites in web browsers.

BinaryAST takes the parsing phase to the server side and delivers an already generated Abstract Syntax Tree (AST). Upon receiving BinaryAST, the browser can immediately go to the compilation stage, without going through JavaScript source code analysis.

A reference implementation prepared under the MIT licenses has been prepared for testing. Node.js components are used to analyze the code to optimize and generate AST, it is written in Rust.

The encoder in BinaryAST can be used both at the end site toolkit level and for packaging external site scripts on the proxy side or content delivery network.

Today, the BinaryAST standardization process by the ECMA TC39 working group has already started, after which the format will be able to coexist with existing methods of compressing the delivered content, such as gzip and brotli.

So many javascript files affect the loading of web pages

Considerable time during JavaScript processing takes the code loading and parsing phase.

Considering the fact that the volume of JavaScript loaded on many popular sites is close to 10 MB (for example, for LinkedIn - 7.2 MB, Facebook - 7.1 MB, Gmail - 3.9 MB), the process JavaScript initial introduces a significant delay.

The analytics stage on the browser side also slows down due to the inability to create an AST on the fly while the code is loading (the browser has to wait for the loading of code blocks, for example the end of functions, to get the missing information to parse the current elements).

Partially, they try to solve the problem by distributing the code in a minimized and compressed form, as well as by caching generated by the browser.

binary-ast-1

Whereas on modern sites the code is updated quite frequently so caching only partially solves the problem.

The solution could be WebAssembly, but it requires the explicit use of writing in the code and it is not well suited to speed up the processing of existing JavaScript code.

Another option is to deliver ready-made compiled code instead of JavaScript scripts, but browser engine developers are opposed to it, since third-party code is difficult to verify, its direct processing can lead to separation from the web. , additional security threats arise.

BinaryAST is the solution

BinaryAST allows integration into the current development model and code delivery without creating a new bytecode and without changing the JavaScript language.

Data size in BinaryAST format is comparable to minified JavaScript code and compressed and the processing speed due to the exclusion of the source analysis phase is remarkably increasing.

Also, the format allows compiling to bytecode as BinaryAST loads, without waiting for all the data to complete.

A feature of BinaryAST is also the ability to restore readable JavaScript, not one-to-one that matches the original version, but is semantically equivalent and includes the same variable and function names (BinaryAST saves the names, but does not save position information in the code, formatting, and comments).

The other side of the coin is the appearance of new vectors for attacks, but according to the developers, they are significantly smaller and more controllable than with the use of alternatives, such as byte code distribution.

Testing of facebook code showed 10-15% of CPU resources they are spent on JavaScript parsing and it takes longer to parse than it does to generate the bytecode and initial code generation for JIT.

The support of BinaryAST is now available in the Nightly versions of Firefox.

Source: https://blog.cloudflare.com


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.