TC39 Binary AST Proposal to Improve JavaScript Performance

By on November 30, 2018 7:00 am

WebAssembly has grown in popularity due to its ability to improve application performance and support transpilation of source code in other languages into something that may get leveraged in a web browser. Every time the JavaScript language gets challenged, the community strives to create mechanisms to improve performance bottlenecks, which we have seen over the years with efforts from Mozilla, Google, Apple, and Microsoft.

One current performance bottleneck with larger JavaScript applications is the time needed to parse the abstract syntax tree (AST). The binary AST seeks to improve the performance of parsing the JavaScript AST by leveraging some of the strategies used in parsing the WebAssembly AST. Championed by engineers from Facebook, Mozilla, and Bloomberg, the proposal notes:

“Even on a powerful laptop, Chrome may spend 15% of CPU time parsing JavaScript while loading the 7MB of uncompressed JavaScript on Facebook.com!”

“Performance of applications on the web platform is becoming bottlenecked by startup (load) time. Larger amounts of JS code are transferred over the wire by more sophisticated web properties. While caching helps, these properties regularly release new code, and cold load times are very important. The performance of application startup degrades as JavaScript payload size increases, and parsing time is a significant portion of an initial application load time. For example, even on a powerful laptop, Chrome may spend 15% of CPU time parsing JavaScript while loading the 7MB of uncompressed JavaScript on Facebook.com!”

Binary AST

The binary AST proposal strives to improve this performance by introducing a new over-the-wire format for JavaScript that provides a binary encoding of an abstract syntax tree (AST). The goal of the proposal is to provide drastically faster parsing. The proposal authors note that because web developers have embraced build tooling like webpack that adoption of this new format should be easy for developers to adopt. Transpilers like TypeScript and Babel could also directly output binary AST.

The proposal offers the starting point of offering a simple alternative encoding of the JavaScript surface syntax with the smallest possible delta to enable high-performance parsing. It does not attempt any semantics-level encoding, such as bytecode or encoding variables instead of identifiers.

Potential solutions for the current parsing bottlenecks include:

  • Information not available where needed (often caused by language features such as variable hoisting or inner functions)
  • JavaScript’s early error semantics (requires the upfront parsing of every JavaScript file)
  • Inefficiencies in using characters (ambiguity at the character-level about what type of expression a JavaScript syntax is encoding)

The binary AST proposal borrows from the WebAssembly parsing approach, with a binary encoding split into three layers:

  • Simple binary encoding of AST nodes using basic primitives
  • Additional structural compression on the previous layer
  • Generic compression algorithm

Prototype

The team proposing binary AST implemented an early prototype using Mozilla’s SpiderMonkey engine by using a grammar based on an internal AST format.

The parsing improvements were much more significant, with the time required to create a full AST reduced by 70-90%.

In looking at the earlier the facebook.com static newsfeed benchmark, the binary AST representation was slightly smaller than the original JavaScript. The parsing improvements were much more significant, with the time required to create a full AST reduced by 70-90%.

FAQs in the proposal explain why it does not consider shipping native bytecode, why WebAssembly is not the answer to all of the web’s problems, and answers to many other questions.

Community reaction

We first learned about binary AST at this year’s TSConf when TypeScript creator Anders Hejlsberg was asked about converting TypeScript to WebAssembly. Anders expressed hope for improvements from binary AST.

At FullStack this summer, I asked JavaScript creator Brendan Eich for his thoughts on binary AST. He remains somewhat skeptical, but feels that if the performance benefits are achievable, then this is a proposal worth strong consideration for a future version of JavaScript.

Conclusion

The binary AST proposal is one of the most promising proposals to improve web performance speed in the past several years. Assuming this proposal progresses further, we expect to use it as soon as it is available as well as support it within Dojo once it is an option.

Need help? Let us know!

While it is too early to leverage binary AST within your applications, we work with many organizations to improve the architecture and performance of their web applications. If you need help improving the performance of your application, contact us to discuss how we can help!