Small speedups for `nitro dev` and `nitro build`

Open
#4,973 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
node.js, typescript, vite

Research direction

The issue groups four CLI speedups and links comparison branches for each; start by reading the linked changes and the related #4939. Check which changes the maintainers want before choosing one, then compare its measurements and scope with the described baseline. Done means the selected optimization is implemented and its reported CLI timing or module loading improvement is confirmed.

Written by the indexing model from the issue text.

Description

cli dev performance

I measured where the Nitro CLI spends its time on a generated app with 300 routes. Each change below is small, so I put them all in one issue instead of opening a PR for each. Tell me which ones you want, and I'll open PRs only for those.

The biggest win, reload after an edit, is in env-runner. It's in unjs/env-runner#104.

1. Turn on Node's compile cache

Each time you run nitro, Node compiles Nitro and its dependencies from scratch. For nitro build that's about 2 MB of JS. module.enableCompileCache() tells Node to save the compiled code to disk and use it again on the next run. Vite's CLI already does this.

main with cache
nitro dev, first response 533 ms 494 ms
nitro build 495 ms 453 ms
nitro preview, first response 143 ms 134 ms

Node writes the cache when the process exits. A dev server doesn't exit, so the branch also writes it 10 seconds after start.

https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/cli-compile-cache

2. Skip Nitro's own imports in the externals check

In dev, the nitro:externals plugin looks at each package import and decides if it stays an import or goes into the bundle. App code imports nitro and nitro/h3 in almost every file, and those always go into the bundle. In the 300-route app the plugin ran 830 times, and only 23 of those runs kept an import.

Adding /^nitro(?:\/|$)/ to noExternal lets Rolldown skip these imports without calling the plugin. The plugin now runs 23 times, and the dev bundle step goes from 155 ms to 138 ms. The Vite builder already has this pattern.

https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/dev-externals-self-imports

3. Let Rolldown skip unwasm's renderChunk

unwasm's renderChunk only changes chunks that import a .wasm file. Rolldown still calls it for every chunk, and it copies the code of each chunk to JS for the call. The 300-route app has 519 chunks, so that's 519 calls that change nothing.

With filter: { code: "unwasm:external:" }, Rolldown skips those chunks. The Rolldown step goes from 262 ms to 252 ms. The same filter on nitro:externals takes it to 242 ms.

https://github.com/unjs/unwasm/compare/main...onmax:unwasm:perf/render-chunk-filter
https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/build-render-chunk-filter

4. Don't load the dev server in nitro build

nitro build imports nitro/builder, and that also loads the dev server and env-runner. If the build command imports the build modules directly, it loads 26 fewer modules. That saves about 12 ms on the 300-route app. It's small, so skip it if you don't like the change. Related to #4939.

https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/build-lazy-dev-modules

Two more ideas I measured but didn't build
  • import("rolldown") waits until the config has loaded, and it takes 15 to 35 ms. It only needs rootDir, so the CLI could start it earlier.
  • copyPublicAssets calls fsp.cp for each file. For 150 files that takes 15 to 35 ms. mkdir plus copyFile takes 4 to 8 ms. But copyFile follows symlinks, and cp copies them as symlinks. So this change would need a check for symlinks.
Dominant language
TypeScript
Stars
11.5k
Forks
968
Avg merge
4d 9h
Merged PRs (30d)
64

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.