Skip to content

Commit 4ca2c8c

Browse files
authored
docs: add experimental options to performance guides (#9743)
1 parent 0cb2f72 commit 4ca2c8c

2 files changed

Lines changed: 103 additions & 8 deletions

File tree

docs/guide/improving-performance.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ export default defineConfig({
7777

7878
You can limit the working directory when Vitest searches for files using [`test.dir`](/config/dir) option. This should make the search faster if you have unrelated folders and files in the root directory.
7979

80+
## Caching Between Reruns
81+
82+
In watch mode, Vitest caches all transformed files in memory, which makes reruns fast. However, this cache is discarded once the test run finishes. By enabling [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache), Vitest persists this cache to the file system so it can be reused across reruns.
83+
84+
This improvement is most noticeable when rerunning a small number of tests that depend on a large module graph. For full test suites, parallelization already mitigates the cost because other tests populate the in-memory cache while earlier tests are still running. For example, running one test file with a huge module graph (>900 modules):
85+
86+
```shell
87+
# the first run
88+
Duration 8.75s (transform 4.02s, setup 629ms, import 5.52s, tests 2.52s, environment 0ms, prepare 3ms)
89+
90+
# the second run
91+
Duration 5.90s (transform 842ms, setup 543ms, import 2.35s, tests 2.94s, environment 0ms, prepare 3ms)
92+
```
93+
8094
## Pool
8195

8296
By default Vitest runs tests in `pool: 'forks'`. While `'forks'` pool is better for compatibility issues ([hanging process](/guide/common-errors.html#failed-to-terminate-worker) and [segfaults](/guide/common-errors.html#segfaults-and-native-code-errors)), it may be slightly slower than `pool: 'threads'` in larger projects.

docs/guide/profiling-test-performance.md

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,101 @@ test('formatter works', () => {
112112
113113
<img src="/module-graph-barrel-file.png" alt="Vitest UI demonstrating barrel file issues" />
114114
115-
To see how files are transformed, you can use `VITEST_DEBUG_DUMP` environment variable to write transformed files in the file system:
115+
To see how files are transformed, you can open the "Module Info" view in the UI:
116+
117+
<img alt="The module info view for an inlined module" img-light src="/ui/light-module-info.png">
118+
<img alt="The module info view for an inlined module" img-dark src="/ui/dark-module-info.png">
119+
120+
## File Import
121+
122+
Some modules just take a long time to load. To identify which modules are the slowest, enable [`experimental.importDurations`](/config/experimental#experimental-importdurations) in your configuration:
123+
124+
```ts [vitest.config.ts]
125+
import { defineConfig } from 'vitest/config'
126+
127+
export default defineConfig({
128+
test: {
129+
experimental: {
130+
importDurations: {
131+
print: true,
132+
},
133+
},
134+
},
135+
})
136+
```
137+
138+
This will print a breakdown of the slowest imports after your tests finish:
139+
140+
```bash
141+
Import Duration Breakdown (Top 10)
142+
143+
Module Self Total
144+
my-test.test.ts 5ms 620ms [████████████████████]
145+
date-fns/index.js 500ms 500ms [████████████████░░░░] # [!code error]
146+
src/utils/helpers.ts 10ms 120ms [████████░░░░░░░░░░░░]
147+
```
148+
149+
You can also use `--experimental.importDurations.print` from the CLI without changing your configuration:
116150
117151
```bash
118-
$ VITEST_DEBUG_DUMP=true vitest --run
152+
vitest --experimental.importDurations.print
153+
```
154+
155+
Once you've identified the slow modules, there are several strategies to speed up imports:
156+
157+
### Use Specific Entry Points
158+
159+
Many libraries ship multiple entry points. Importing the main entry point (which is often a [barrel file](https://vitejs.dev/guide/performance.html#avoid-barrel-files)) can pull in far more code than you need.
119160
120-
RUN v2.1.1 /x/vitest/examples/profiling
121-
...
161+
For example, `date-fns` re-exports hundreds of functions from its main entry point. Instead of importing from the top-level module, import directly from the specific function:
122162
123-
$ ls .vitest-dump/
124-
_x_examples_profiling_global-setup_ts-1292904907.js
125-
_x_examples_profiling_test_prime-number_test_ts-1413378098.js
126-
_src_prime-number_ts-525172412.js
163+
```ts
164+
import { format } from 'date-fns' // [!code --]
165+
import { format } from 'date-fns/format' // [!code ++]
127166
```
128167
168+
### Use `resolve.alias` to Redirect Imports
169+
170+
If a dependency doesn't provide granular entry points, or if third-party code imports the heavy entry point, you can use [`resolve.alias`](https://vite.dev/config/shared-options#resolve-alias) to redirect imports to a lighter alternative:
171+
172+
```ts [vitest.config.ts]
173+
import { defineConfig } from 'vitest/config'
174+
175+
export default defineConfig({
176+
resolve: {
177+
alias: [
178+
{
179+
find: /^date-fns$/,
180+
replacement: join(dirname(require.resolve('date-fns/package.json')), 'index.cjs'),
181+
},
182+
]
183+
},
184+
})
185+
```
186+
187+
### Use the Dependency Optimizer
188+
189+
Vitest can bundle external libraries into a single file using [`deps.optimizer`](/config/deps#deps-optimizer), which reduces the overhead of importing packages with many internal modules:
190+
191+
```ts [vitest.config.ts]
192+
import { defineConfig } from 'vitest/config'
193+
194+
export default defineConfig({
195+
test: {
196+
deps: {
197+
optimizer: {
198+
ssr: {
199+
enabled: true,
200+
include: ['date-fns'],
201+
},
202+
},
203+
},
204+
},
205+
})
206+
```
207+
208+
This is especially effective for UI libraries and packages with deep import trees. Use `optimizer.ssr` for `node`/`edge` environments and `optimizer.client` for `jsdom`/`happy-dom` environments.
209+
129210
## Code Coverage
130211
131212
If code coverage generation is slow on your project you can use `DEBUG=vitest:coverage` environment variable to enable performance logging.

0 commit comments

Comments
 (0)