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

Abierto
#4,973 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
42/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
node.js, typescript, vite

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
TypeScript
Estrellas
11.5k
Forks
968
Merge medio
4 d 9 h
PR fusionados (30 d)
64

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de nitrojs/nitro

Todos los issues de nitrojs/nitro

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.