BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Parcel 2: Rust-Based 10x Faster JavaScript Compiler, Modular Configuration, Differential Bundling

Parcel 2: Rust-Based 10x Faster JavaScript Compiler, Modular Configuration, Differential Bundling

This item in japanese

The team behind the zero-config bundler Parcel recently announced the first Parcel 2 release candidate. Parcel 2 is a ground-up rewrite of Parcel that has been more than three years in the making. Parcel’s new JavaScript compiler is written in Rust and boasts 10x build performance improvement. Parcel 2 is fully extensible, supports automatic differential bundling, and much more.

The team explained the rationale behind the JavaScript compiler rewrite as follows:

While we had made great progress optimizing [miscellaneous bundling stages], JavaScript compilation was still by far the slowest part of Parcel.
In particular, serializing large JavaScript ASTs to send between threads was very slow, and these large objects put a lot of pressure on the JavaScript garbage collector. Also, JavaScript code must be compiled by the engine every time it is run, which means it is slow to start up. While improving algorithmic complexity will result in performance improvements regardless of the language, that can only take you so far.

The Parcel team thus leveraged the swc compiler. swc is a TypeScript / JavaScript compiler written in Rust that claims to be 20x faster than the Babel JavaScript compiler on single thread executions, and 70x faster on 4-core benchmarks. Parcel’s new JavaScript transformer is written in Rust, on top of swc. Besides JavaScript, swc also replaces Babel by default for transpilation when a browserslist is set, as well as for compiling JSX and TypeScript, and React Fast Refresh.

The team provided the following performance benchmark, which indicates that Parcel is about 10x faster without minification, and ~3x faster with minification enabled:

Chart showing performance of previous beta vs rust rewrite. Without terser, previous beta took 34s and the rust rewrite took 3.2s. With terser, the previous beta took 56s and the rust rewrite took 18s.

The Deno team similarly switched last year to the swc JavaScript compiler (Deno 1.5 release). The Deno team reported a 3x bundling performance improvement (15x without type-checking).

While Parcel famously requires no configuration from developers by default, Parcel 2 is built around a modular design that enables user-defined plugins that can customize various stages of the bundling pipeline. The Parcel team explains:

Parcel famously requires zero configuration. This never meant non-configurable, just that Parcel attempted to infer as much as possible from the code itself and used existing config files for other tools (e.g. .babelrc). Users have loved the ease of use and developer experience that this has provided, but there are always edge cases and real use cases that require a bit more customizability. […]
Parcel 2 supports extending the core with additional functionality through a complete set of plugin types designed specifically for each of Parcel’s core stages.

Developers can extend Parcel’s core functionalities in any stage of the pipeline. Plugins types are named in correspondence with the pipeline stage that they customize. Plugins may thus be called resolvers, transformers, bundlers, namers, runtimes, packagers, optimizers, validators, and reporters. Resolvers for instance resolve import './foo' into full file paths like /my/project/foo.js.

Parcel 2 allows developers to configure builds for multiple targets which will be built in parallel. Parcel 2 automatically generates both native ES modules for modern browsers and fallback classic scripts for old browsers. The Parcel team emphasized the performance benefits of differential bundling:

This reduces bundle sizes significantly for a majority of users by shipping modern syntax rather than transpiling to ES5.

Parcel 2 has many more features, improvements, and bug fixes (e.g. improved JSX support, improved web worker support). The Parcel team is still working on the final Parcel 2 release and the corresponding release note. In the meanwhile, interested developers will find plenty of details in the previous Parcel release notes: Parcel alpha 1, Parcel beta 1, Parcel beta 3, and Parcel 2 RC.

Rate this Article

Adoption
Style

BT