BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Sucrase, a Faster Babel for Modern JS Runtimes

Sucrase, a Faster Babel for Modern JS Runtimes

This item in japanese

Bookmarks

The Sucrase JavaScript/TypeScript compiler aims at providing significantly faster development builds than the standard Babel compiler. Faster builds mean faster iterations, specially when testing on a large codebase. Some empirical measures show a gain in speed between 4x and 20x vs. Babel. Developers may thus enjoy the improved developer experience in development, and still resort to Babel in production.

Sucrase describes itself as a "super-fast Babel alternative" targeting development builds. By making some restrictive assumptions, Sucrase may achieve significant speed gains vs. Babel. Developers may evaluate the speed gain on Sucrase's website by supplying TypeScript code and observing both results of Sucrase and Babel compilers. The default example shows speed gains varying between a 4-time and 7-time speed gain on a 20 line-of-code source. Sucrase provides in its GitHub project the example of a large TypeScript codebase with 661K lines of code, which Sucrase compiled 20-times faster than Babel:

Imgur

The observed speed gains are achieved by a reduction in scope of the compiler goals, and an architecture sacrificing maintainability for performance. Sucrase explains the scope reduction as follows:

Instead of compiling a large range of JS features to be able to work in Internet Explorer, Sucrase assumes that you're developing with a recent browser or recent Node.js version, so it focuses on compiling non-standard language extensions: JSX, TypeScript, and Flow.

Sucrase does not support Node 6 or Internet Explorer, and claims to only reluctantly support CommonJS and legacy TypeScript and Babel 5 module interop. As recent browsers natively support a large swath of the new features included in the JavaScript language, and assuming usage in a development and testing context, the Sucrase compiler can afford to focus on a smaller set of language features. Aside from the aforementioned JSX, TypeScript and Flow, the proposed JavaScript features are taken into account when compiling:

Specific transforms are also included to deal with ES Modules (import/export) and development with React (react-hot-loader). Any JavaScript syntax not mentioned above will "pass through" as-is and needs to be supported by the used JavaScript runtime. Developers should check that they are using only language features supported by modern browsers (e.g. a subset of Stage 4 ECMAScript features). As Sucrase does not check a codebase for errors, it should be used with a linter or typechecker.

Architecture-wise, Sucrase's syntax tree transforms are intricately coupled for better performance. As a result, Sucrase does not support plugins and is hard to extend with new language extensions and upcoming language features.

Sucrase seeks to provide a better developer experience, specially on large codebases, where build times can be slow, and the caches used to remediate that problem may themselves be fragile, occasionally requiring a slow clean build. The problem is specially acute for testing, which is a highly iterative process, and may benefit from improved compilation cycles.

Sucrase does not however intend to be a replacement for Babel. The documentation mentions:

You should think carefully before using Sucrase in production. Sucrase is mostly beneficial in development, and in many cases, Babel or tsc will be more suitable for production builds.

Sucrase is MIT-licensed. A large part of Sucrase is based on a fork of the Babel parser, which is also MIT-licensed. Contributions are welcome, whether in the form of bug reports, pull requests, documentation, or tests. Contributions must follow the Contributing Guide.

Rate this Article

Adoption
Style

BT