Small speedups for `nitro dev` and `nitro build`
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
- Domain
- backend, build-system, performance
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
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 needsrootDir, so the CLI could start it earlier.copyPublicAssetscallsfsp.cpfor each file. For 150 files that takes 15 to 35 ms.mkdirpluscopyFiletakes 4 to 8 ms. ButcopyFilefollows symlinks, andcpcopies 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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nitrojs/nitro
-
pending triage
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 2 days
-
cloudflare: `nitro deploy` fails when a parent directory has a Wrangler configPossibly taken @draarivpatel-ui claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 2 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
pending triage
Difficulty 5/5 Over a week Newbie friendliness 30/100
nitrojs/nitro#5205 · 1 reaction ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ranxianglei/billion-context#8319 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
JimLiu/open-artifact#126 ·
-
bug repro-ready
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
mksglu/context-mode#1454 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
laststance/design-world#98 ·
Maintainers usually reply within 1 day
-
Add: KCM True crimeOpenapproved check:passed streams:add
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day