Engineers from Cloudflare, Mozilla, Facebook, and Bloomberg new format to accelerate the delivery and processing of JavaScript code when opening websites in browsers. BinaryAST offloads the parsing phase to the server and delivers an already formed abstract syntax tree (). When receiving BinaryAST, the browser can immediately move to the compilation stage, bypassing the parsing of the JavaScript source code.
For testing a reference implementation provided under an MIT license. Parsing uses components of Node.js, and the code for optimizing and generating the AST is written in Rust. On the browser side, support for
BinaryAST is already available in Firefox. The encoder in BinaryAST can be applied at both the level of the site's toolset and for packaging scripts from external sites on the proxy or content delivery network side. The standardization process for BinaryAST has already begun by the , after which the format will be able to coexist with existing methods of content compression, such as gzip and brotli.
A significant amount of processing time for JavaScript is spent in the loading and parsing phases. Given that the size of the JavaScript being loaded on many popular sites approaches 10 MB (for example, LinkedIn — 7.2 MB, Facebook — 7.1 MB, Gmail — 3.9 MB), the initial processing of JavaScript introduces considerable delays. The parsing stage on the browser side is also slowed down by the inability to fully build the AST on the fly as the code is loaded (the browser has to wait for the loading of code blocks, such as the end of functions, to obtain the missing information needed for parsing current elements).
Part of the problem is being addressed by distributing the code in minimized and compressed form, as well as through the caching of the generated bytecode by the browser. On modern sites, the code is updated quite frequently, thus caching only partially solves the problem. WebAssembly could be a solution, but it requires explicit typing in the code and is not well-suited for speeding up the processing of already existing JavaScript code.
An alternative is to deliver pre-compiled bytecode instead of JavaScript scripts, but browser engine developers oppose this because third-party bytecode is difficult to verify, direct processing can lead to web fragmentation, additional security threats arise, and a universal bytecode format needs to be developed.
BinaryAST allows integration into the current code development and delivery model without creating new bytecode or modifying the JavaScript language. The size of data in the BinaryAST format is comparable to that of compressed minified JavaScript code, and processing speed increases significantly due to the elimination of the source text parsing phase. Additionally, the format allows for bytecode compilation as BinaryAST is loaded, without waiting for all data to be fully received. Furthermore, server-side parsing allows the removal of unused functions and extraneous code from the delivered BinaryAST representation, which consumes time both for parsing and excess traffic when done in the browser.
A feature of BinaryAST is also the ability to restore readable JavaScript that does not exactly match the original version, but is semantically equivalent and includes the same variable and function names (BinaryAST preserves names but does not retain position information, formatting, and comments). The downside, however, is the emergence of new attack vectors, but developers believe they are significantly smaller and more controllable than when using alternatives like bytecode distribution.
Tests on the code of facebook.com showed that 10-15% of CPU resources are spent on parsing JavaScript, and more time is spent on parsing than on bytecode generation and initial code formation for JIT. In the SpiderMonkey engine, the total AST construction time takes 500-800 ms, and the use of BinaryAST has reduced this figure by 70-90%.
Overall, for most web frameworks, using BinaryAST reduces JavaScript parsing time by 3-10% in non-optimized mode and by 90-97% when the mode for ignoring unused functions is enabled.
When executing a test JavaScript set of 1.2 MB, the use of BinaryAST reduced the startup time from 338 to 314 ms on a desktop system (Intel i7) and from 2019 to 1455 ms on a mobile device (HTC One M8).
Source: opennet.ru
