Skip to content

Commit 2b9b3e4

Browse files
sokraskipjack
authored andcommitted
docs(guides): add build-performance page (#1506)
This adds a solid baseline for how to start improving build performance with specific pointers for development and production. It can be built on in other PRs but probably doesn't need to be synchronized with the earlier guides (as it's a lot of one off changes that depend on your use case).
1 parent 13661b9 commit 2b9b3e4

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

content/guides/build-performance.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
---
2+
title: Build Performance
3+
sort: 17
4+
contributors:
5+
- sokra
6+
---
7+
8+
This guide contains some useful tips for improving build/compilation performance.
9+
10+
---
11+
12+
## General
13+
14+
The following best practices should help whether or not you are in [development](/guides/development) or building for [production](/guides/production).
15+
16+
17+
### Stay Up to Date
18+
19+
Use the latest webpack version. We are always making performance improvements. The latest stable version of webpack is:
20+
21+
[![latest webpack version](https://img.shields.io/npm/v/webpack.svg?label=webpack&style=flat-square&maxAge=3600)](https://github.com/webpack/webpack/releases)
22+
23+
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.
24+
25+
26+
### Loaders
27+
28+
Apply loaders to the minimal number of modules necessary. Instead of:
29+
30+
``` js
31+
{
32+
test: /\.js$/,
33+
loader: "babel-loader"
34+
}
35+
```
36+
37+
Use the `include` field to only apply the loader modules that actually need to be transformed by it:
38+
39+
``` js
40+
{
41+
test: /\.js$/,
42+
include: path.resolve(__dirname, "src"),
43+
loader: "babel-loader"
44+
}
45+
```
46+
47+
48+
### Bootstrap
49+
50+
Each additional loader/plugin has a bootup time. Try to use a few different tools are possible.
51+
52+
53+
### Resolving
54+
55+
The following steps can increase the speed of resolving:
56+
57+
- Minimize the number of items in `resolve.modules`, `resolve.extensions`, `resolve.mainFiles`, `resolve.descriptionFiles` as they increase the number of filesystem calls.
58+
- Set `resolve.symlinks: false` if you don't use symlinks (e.g. `npm link` or `yarn link`).
59+
- Set `resolve.cacheWithContext: false` if you use custom resolving plugins, that are not context specific.
60+
61+
62+
### Dlls
63+
64+
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 complexitity of the build process.
65+
66+
67+
### Smaller = Faster
68+
69+
Decrease the total size of the compilation to increase build performance. Try to keep chunks small.
70+
71+
- Use less/smaller libraries.
72+
- Use the `CommonsChunksPlugin` in Multi-Page Applications.
73+
- Use the `CommonsChunksPlugin` in `async` mode in Multi-Page Applications.
74+
- Remove unused code.
75+
- Only compile the part of the code you are currenly developing on.
76+
77+
78+
### Worker Pool
79+
80+
The `thread-loader` can be used to offload expensive loaders to a worker pool.
81+
82+
W> 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.
83+
84+
85+
### Persistent cache
86+
87+
Enable persistent caching with the `cache-loader`. Clear cache directory on `"postinstall"` in `package.json`.
88+
89+
90+
### Custom plugins/loaders
91+
92+
Profile them to not intruduce a performance problem here.
93+
94+
---
95+
96+
97+
## Development
98+
99+
The following steps are especially useful in _development_.
100+
101+
102+
### Incremental Builds
103+
104+
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.
105+
106+
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`.
107+
108+
109+
### Compile in Memory
110+
111+
The following utilities improve performance by compiling and serving assets in memory rather than writing to disk:
112+
113+
- `webpack-dev-server`
114+
- `webpack-hot-middleware`
115+
- `webpack-dev-middleware`
116+
117+
118+
### Devtool
119+
120+
Be aware of the performance differences of the different `devtool` settings.
121+
122+
- `"eval"` has the best performance, but doesn't assist you for transpilied code.
123+
- The `cheap-source-map` variants are more performant, if you can live with the slightly worse mapping quality.
124+
- Use a `eval-source-map` variant for incremental builds.
125+
126+
=> In most cases `eval-cheap-module-source-map` is the best option.
127+
128+
129+
### Avoid Production Specific Tooling
130+
131+
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:
132+
133+
- `UglifyJsPlugin`
134+
- `ExtractTextPlugin`
135+
- `[hash]`/`[chunkhash]`
136+
- `AggressiveSplittingPlugin`
137+
- `AggressiveMergingPlugin`
138+
- `ModuleConcatenationPlugin`
139+
140+
141+
### Minimal Entry Chunk
142+
143+
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.
144+
145+
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_:
146+
147+
``` js
148+
new CommonsChunkPlugin({
149+
name: "manifest",
150+
minChunks: Infinity
151+
})
152+
```
153+
154+
---
155+
156+
157+
## Production
158+
159+
The following steps are especially useful in _production_.
160+
161+
W> __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.
162+
163+
164+
### Multiple Compilations
165+
166+
When using multiple compilations the following tools can help:
167+
168+
- [`parallel-webpack`](https://github.com/trivago/parallel-webpack): It allows to do compilation in a worker pool.
169+
- `cache-loader`: The cache can be shared between multiple compilations.
170+
171+
172+
### Source Maps
173+
174+
Source maps are really expensive. Do you really need them?
175+
176+
---
177+
178+
179+
## Specific Tooling Issues
180+
181+
The following tools have certain problems that can degrade build performance.
182+
183+
184+
### Babel
185+
186+
- Minimize the number of preset/plugins
187+
188+
189+
### Typescript
190+
191+
- Use the `fork-ts-checker-webpack-plugin` for type checking in a separate process.
192+
- Configure loaders to skip typechecking.
193+
- Use the `ts-loader` in `happyPackMode: true` / `transpileOnly: true`.
194+
195+
196+
### Sass
197+
198+
- `node-sass` has a bug which blocks threads from the Node.js threadpool. When using it with the `thread-loader` set `workerParallelJobs: 2`.
199+

0 commit comments

Comments
 (0)