Build Performance

ReScript considers performance at install time, build time and run time as a serious feature; it's one of those things you don't notice until you realize it's missing.

Measuring Build Times

When investigating a slowdown, compare the same kinds of build under the same conditions:

  • a clean build

  • a no-op build, with no source changes

  • an incremental build after changing a leaf module

  • an incremental build after changing a widely used module

Build times depend on the dependency graph, hardware, filesystem, and surrounding tools. Compare ReScript's build time with the duration of the entire development command: bundling, code generation, and tests can take more time than compilation itself.

Under the Hood

rescript build runs a native build system written in OCaml. It reads rescript.json, tracks dependencies between modules and packages, and compiles modules in-process on several CPU cores. Each build prints how long it took; pass --no-timing to turn that off.

The JS Wrapper

The rescript command is a thin Node.js wrapper around the native rescript.exe binary. Both building and watching are handled by the native binary.

The binary ships in a platform-specific package at node_modules/@rescript/{your-platform}/bin/rescript.exe, for example node_modules/@rescript/darwin-arm64/bin/rescript.exe on an Apple Silicon Mac. The wrapper selects the package for your platform and sets RESCRIPT_RUNTIME to the installed runtime package's path. Prefer rescript build and rescript watch so this setup is handled for you.

Incrementality & Correctness

ReScript doesn't take whole seconds to run every time. The bulk of the build performance comes from incremental build, aka re-building a previously built project when a few files changed.

In short, thanks to our compiler and the build system's architecture, we're able to only build what's needed. If MyFile.res isn't changed, it isn't recompiled. Renaming or moving files is handled automatically, with no stale builds.

Speed Up Incremental Build

ReScript uses the concept of interface files (.resi) (or, equivalently, module signatures). Exposing only what you need naturally speeds up incremental builds. E.g. if you change a .res file whose corresponding .resi file doesn't expose the changed part, then you've reduced the amount of dependent files you have to rebuild.

Programmatic Usage

Unfortunately, JS build systems are usually the bottleneck for building a JS project nowadays. Having parts of the build blazingly fast doesn't matter much if the rest of the build takes seconds or literally minutes. Here are a few suggestions:

  • Convert more files into ReScript =). Fewer files going through fewer parts of the JS pipeline helps a ton.

  • Careful with bringing in more dependencies: libraries, syntax transforms (e.g. the unofficially supported PPX), build step loaders, etc. The bulk of these dragging down the editing & building experience might out-weight the API benefits they provide.

Keep compiler artifacts between builds unless you specifically need a clean build. Cleaning before every compilation prevents incremental builds from doing their job.

Commands configured through js-post-build run once per emitted JavaScript file. Starting another process for each file can slow down an otherwise fast incremental build.

Hot Reloading

Hot reloading refers to maintaining a dev server and listening to file changes in a way that allows the server to pipe some delta changes right into the currently running browser page. This provides a relatively fast iteration workflow while working in specific frameworks.

However, hot reloading is fragile by nature, and counts on the occasional inconsistencies (bad state, bad eval, etc.) and the heavy devserver setup/config being less of a hassle than the benefits it provides. We err on the side of caution and stability in general, and decided not to provide a built-in hot reloading yet. Note: you can still use the hot reloading facility provided by your JS build pipeline.