"Of JavaScript Ahead-Of-Time Compilation Performance" by Manuel Serrano (Strange Loop 2022)

Explore how JavaScript's dynamic typing affects ahead-of-time compilation performance and discover techniques used to optimize function calls, type inference, and code generation for various JavaScript patterns.

Key takeaways
  • JavaScript is dynamically typed, so the first step is to verify if F is a function accepting two arguments.
  • If F is a function, the next step is to check if the arguments are of the optimized type.
  • If the arguments are of the optimized type, the compiler can branch directly to the optimized version of the function.
  • If the arguments are not of the optimized type, the compiler will generate a dispatch to the generic version of the function.
  • The compiler uses a set of rules to infer the most general type for each variable.
  • The compiler generates several versions of each function, one for each possible type.
  • At runtime, the system selects the most efficient version of the function based on the types of the arguments.
  • The compiler uses opportunistic compilation to optimize code that is not frequently executed.
  • The compiler uses profile-guided optimization to improve the performance of code that is frequently executed.
  • The compiler is able to generate code that is as efficient as hand-written C code for many common JavaScript patterns.
  • The compiler is still under development, but it is already able to compile a wide range of JavaScript programs.