This guide contains some useful tips for improving build/compilation performance.
The following best practices should help whether or not you are in development or building for production.
Use the latest webpack version. We are always making performance improvements. The latest stable version of webpack is:
Staying up to date with Node.js can also help with performance. On top of this, keeping your package manager (e.g. npm
or yarn
) up to date can also help. Newer versions create more efficient module trees and increase resolving speed.
Apply loaders to the minimal number of modules necessary. Instead of:
{
test: /\.js$/,
loader: "babel-loader"
}
Use the include
field to only apply the loader modules that actually need to be transformed by it:
{
test: /\.js$/,
include: path.resolve(__dirname, "src"),
loader: "babel-loader"
}
Each additional loader/plugin has a bootup time. Try to use as few different tools as possible.
The following steps can increase the speed of resolving:
resolve.modules
, resolve.extensions
, resolve.mainFiles
, resolve.descriptionFiles
as they increase the number of filesystem calls.resolve.symlinks: false
if you don't use symlinks (e.g. npm link
or yarn link
).resolve.cacheWithContext: false
if you use custom resolving plugins, that are not context specific.Use the DllPlugin
to move code that is changed less often into a separate compilation. This will improve the application's compilation speed, although it does increase complexity of the build process.
Decrease the total size of the compilation to increase build performance. Try to keep chunks small.
CommonsChunksPlugin
in Multi-Page Applications.CommonsChunksPlugin
in async
mode in Multi-Page Applications.The thread-loader
can be used to offload expensive loaders to a worker pool.
Don't use too many workers as there is a boot overhead for the Node.js runtime and the loader. Minimize the module transfers between worker and main process. IPC is expensive.
Enable persistent caching with the cache-loader
. Clear cache directory on "postinstall"
in package.json
.
Profile them to not introduce a performance problem here.
The following steps are especially useful in development.
Use webpack's watch mode. Don't use other tools to watch your files and invoke webpack. The built in watch mode will keep track of timestamps and passes this information to the compilation for cache invalidation.
In some setups watching falls back to polling mode. With many watched files this can cause a lot of CPU load. In these cases you can increase the polling interval with watchOptions.poll
.
The following utilities improve performance by compiling and serving assets in memory rather than writing to disk:
webpack-dev-server
webpack-hot-middleware
webpack-dev-middleware
Be aware of the performance differences of the different devtool
settings.
"eval"
has the best performance, but doesn't assist you for transpiled code.cheap-source-map
variants are more performant, if you can live with the slightly worse mapping quality.eval-source-map
variant for incremental builds.=> In most cases cheap-module-eval-source-map
is the best option.
Certain utilities, plugins and loader only make sense when building for production. For example, it usually doesn't make sense to minify and mangle your code with the UglifyJsPlugin
while in development. These tools should typically be excluded in development:
UglifyJsPlugin
ExtractTextPlugin
[hash]
/[chunkhash]
AggressiveSplittingPlugin
AggressiveMergingPlugin
ModuleConcatenationPlugin
webpack only emits updated chunks to the filesystem. For some configuration options (HMR, [name]
/[chunkhash]
in output.chunkFilename
, [hash]
) the entry chunk is invalidated in addition to the changed chunks.
Make sure the entry chunk is cheap to emit by keeping it small. The following code block extracts a chunk containing only the runtime with all other chunks as children:
new CommonsChunkPlugin({
name: "manifest",
minChunks: Infinity
})
The following steps are especially useful in production.
Don't sacrifice the quality of your application for small performance gains! Keep in mind that optimization quality is in most cases more important than build performance.
When using multiple compilations the following tools can help:
parallel-webpack
: It allows to do compilation in a worker pool.cache-loader
: The cache can be shared between multiple compilations.Source maps are really expensive. Do you really need them?
The following tools have certain problems that can degrade build performance.
fork-ts-checker-webpack-plugin
for type checking in a separate process.ts-loader
in happyPackMode: true
/ transpileOnly: true
.node-sass
has a bug which blocks threads from the Node.js threadpool. When using it with the thread-loader
set workerParallelJobs: 2
.