一句话:提出静态分析与代码变换方法,自动为 JavaScript 应用引入第三方包懒加载,减少初始体积和加载时间。
前端 JavaScript 应用常因大量复用第三方包而体积膨胀,降低页面响应速度。虽然 2020 年加入的异步动态 import 支持按需懒加载外部包,但现有项目迁移需要复杂且非局部的代码改写。本文提出自动引入第三方包懒加载的方法:用静态分析识别可被懒加载的外部件,并生成相应代码变换;由于静态分析不完备,这些变换作为建议供程序员审查和测试。作者实现工具 Lazifier,在 10 个开源前端 JavaScript 应用上评估。所有应用均被成功重构,平均初始体积减少 36.2%,初始加载时间减少 29.7%,且未出现不完备性问题。
Front-end developers want their applications to contain no more code than is needed in order to minimize the amount of time that elapses between visiting a web page and the page becoming responsive. However, front-end code is typically written in JavaScript, the ubiquitous “language of the web”, and tends to rely heavily on third-party packages. While the reuse of packages improves developer productivity, it is notorious for resulting in very large “bloated” applications, resulting in a degraded end-user experience. One way to combat such bloat is to lazily load external packages on an as-needed basis, for which support was added to JavaScript in 2020 when asynchronous, dynamic imports were added to the language standard. Unfortunately, migrating existing projects to take advantage of this feature is nontrivial, as the code changes required to introduce asynchrony may involve complex, non-local transformations. In this work, we propose an approach for automatically introducing lazy loading of third-party packages in JavaScript applications. Our approach relies on static analysis to identify external packages that can be loaded lazily and generates the code transformations required to lazily load those packages. Since the static analysis is unsound, these transformations are presented as suggestions that programmers should review and test carefully. We implement this approach in a tool called Lazifier, and evaluate Lazifier on 10 open-source front-end JavaScript applications, showing that each application was successfully refactored, reducing initial application size and load times in all cases. On average, for these applications, Lazifier reduces initial application size by 36.2 %, initial load time by 29.7 %, and unsoundness did not arise in any of these applications.