> For AI agents: the complete documentation index is available at https://rspack.rs/llms.txt, the full documentation bundle is available at https://rspack.rs/llms-full.txt.

# Build performance profiling

This chapter introduces some common performance bottlenecks and performance profile methods for Rspack.

For Rust data-structure snapshots and comparisons across HMR updates, see [Memory analysis](https://rspack.rs/guide/diagnostics/memory.md).

## Analysis with Rsdoctor

[Rsdoctor](https://rsdoctor.rs/) is a build analyzer that can visually display the build process, such as compilation time, code changes before and after compilation, module reference relationships, duplicate modules, etc.

Please refer to [Use Rsdoctor](https://rspack.rs/guide/diagnostics/use-rsdoctor.md) for more information.

## Rspack profile

The Rspack CLI supports the use of the `RSPACK_PROFILE` environment variable for build performance profile.

```sh
RSPACK_PROFILE=ALL rspack build
```

This command will generate a `.rspack-profile-${timestamp}-${pid}` folder. By default, it contains the `rspack.log` file, which is generated by Rspack based on [tracing](https://github.com/tokio-rs/tracing) and records build timing information as JSON Lines.

`@rspack-debug/core` is a diagnostic variant of `@rspack/core` that includes extra debugging and tracing capabilities. If you use `@rspack-debug/core` and set `RSPACK_TRACE_LAYER=perfetto`, the profile directory will contain `rspack.pftrace`, which can be viewed using [ui.perfetto.dev](https://ui.perfetto.dev/).

> See [Tracing](https://rspack.rs/contribute/development/tracing.md) for more information.

## Loader performance

Loader performance depends on the files they process and the transformations they apply. Narrowing their scope or using Rspack's built-in alternatives can reduce this overhead.

### babel-loader

[babel-loader](https://github.com/babel/babel-loader) compiles JavaScript and TypeScript code using Babel. You can replace Babel with the faster SWC. Rspack comes with a built-in [builtin:swc-loader](https://rspack.rs/guide/builtin-loaders/swc-loader.md), which is the Rust version of `swc-loader` and is intended to provide better performance.

If you need to use some Babel plugins for custom transformations, configure babel-loader with [rules\[\].include](https://rspack.rs/config/module-rules.md#rulesinclude) to match as few files as possible to reduce the Babel compilation overhead.

### postcss-loader

[postcss-loader](https://github.com/postcss/postcss-loader) compiles CSS code based on PostCSS, which is often used with PostCSS plugins to downgrade CSS syntax, add vendor prefixes, etc. You can replace PostCSS with the faster Lightning CSS by using Rspack's built-in [builtin:lightningcss-loader](https://rspack.rs/guide/builtin-loaders/lightningcss-loader.md).

### less-loader

[less-loader](https://github.com/webpack/less-loader) compiles `.less` files based on Less. Since Less currently lacks an officially implemented high performance alternative, it is recommended to use [sass-loader](https://github.com/webpack/sass-loader) and [sass-embedded](https://www.npmjs.com/package/sass-embedded) instead. `sass-embedded` is a JavaScript wrapper for Sass's native Dart executable that provides excellent performance.

## Plugin performance

Plugins run tasks at different stages of a build. Use profiling tools to identify expensive plugins, then consider adjusting their configuration or switching to faster alternatives.

### terser-webpack-plugin

[terser-webpack-plugin](https://github.com/webpack/terser-webpack-plugin) minifies JavaScript code based on Terser. You can replace Terser with the faster SWC minimizer by using Rspack's built-in [SwcJsMinimizerRspackPlugin](https://rspack.rs/plugins/swc-js-minimizer-rspack-plugin.md).

### css-minimizer-webpack-plugin

[css-minimizer-webpack-plugin](https://github.com/webpack/css-minimizer-webpack-plugin) minifies CSS code based on tools like cssnano. You can replace cssnano with the faster Lightning CSS minimizer by using Rspack's built-in [LightningCssMinimizerRspackPlugin](https://rspack.rs/plugins/lightning-css-minimizer-rspack-plugin.md).

### html-webpack-plugin

[html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) performs poorly when generating large numbers of HTML files. The [HtmlRspackPlugin](https://rspack.rs/plugins/html-rspack-plugin.md) implemented in Rust by Rspack can provide better performance.

### ts-checker-rspack-plugin

When using [ts-checker-rspack-plugin](https://github.com/rspack-contrib/ts-checker-rspack-plugin), TypeScript 7 or later is recommended to enable the native type checker and improve type-checking performance on large projects. See the [plugin README](https://github.com/rspack-contrib/ts-checker-rspack-plugin#typescript-7-support) for details.

## Configuration and APIs

### Stats generation

Requesting detailed stats with [`stats.toJson()`](https://rspack.rs/api/javascript-api/stats.md#tojson) or [`stats.toString()`](https://rspack.rs/api/javascript-api/stats.md#tostring) can add noticeable overhead, especially with [`all: true`](https://rspack.rs/config/stats.md#statsall). Check these calls in build scripts and plugins, and request only the information you need. See [Stats performance overhead](https://rspack.rs/api/javascript-api/stats.md#performance-overhead) for usage recommendations.

### SplitChunks

Function options in [`splitChunks`](https://rspack.rs/plugins/split-chunks-plugin.md) and a higher [`dedupDepth`](https://rspack.rs/plugins/split-chunks-plugin.md#splitchunksdedupdepth) can increase build time. See [SplitChunks performance overhead](https://rspack.rs/plugins/split-chunks-plugin.md#performance-overhead) for the causes and configuration recommendations.

### Module rules

Using functions for module rule options such as [`test`](https://rspack.rs/config/module-rules.md#rulestest), [`include`](https://rspack.rs/config/module-rules.md#rulesinclude), [`exclude`](https://rspack.rs/config/module-rules.md#rulesexclude), or [`use`](https://rspack.rs/config/module-rules.md#rulesuse) can add overhead during module processing. Prefer non-function forms when they meet your needs. See [Module rules performance overhead](https://rspack.rs/config/module-rules.md#performance-overhead) for details.

## Thread pool configuration

### Blocking thread pool size

Rspack internally uses a dedicated thread pool to handle blocking operations such as file system reads and writes, preventing the main thread from being blocked.

By default, the pool size is set to `4`, matching Node.js's [libuv](https://docs.libuv.org/en/v1.x/threadpool.html) behavior, which provides stable performance across most development and CI environments.

If your build environment uses high-speed storage, you can adjust the thread count via the `RSPACK_BLOCKING_THREADS` environment variable to improve parallelism and potentially reduce build time, for example:

```bash
# Need to install cross-env package to set environment variables
cross-env RSPACK_BLOCKING_THREADS=8 rspack build
```

After setting the variable, observe the build time to find the most suitable configuration.

On slower or high-latency file systems, it's recommended to keep the default value or even lower it to avoid thread contention.

### Working thread pool size

Rspack internally uses a Tokio thread pool and Rayon thread pool to handle CPU bound tasks (Tokio for async tasks and Rayon for sync tasks). By default, Tokio and Rayon automatically determines the number of worker threads based on the number of CPU cores available.

If it consumes too much CPU resources during the build process, you can limit the number of worker threads by setting the `TOKIO_WORKER_THREADS` and `RAYON_NUM_THREADS` environment variable, for example:

```bash
# Need to install cross-env package to set environment variables
cross-env TOKIO_WORKER_THREADS=4 RAYON_NUM_THREADS=4 rspack build
```

Be aware that setting this value too low may lead to longer build times. After setting the variable, observe the build time to find the most suitable configuration.
