mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-11-25 18:23:46 +03:00
[Perf] nvm_alias, nvm_normalize_lts: avoid forks on alias lookup
Resolving `default -> lts/*` at startup walks default -> lts/* -> lts/<name> -> version, and each `nvm_alias` step forked: `$(nvm_normalize_lts)` even when nothing is rewritten, `echo | tr` to check for lowercase, and `$(nvm_version_dir old)` inside `nvm_alias_path`.
- `nvm_alias_path`: use `${NVM_DIR}` directly, as `nvm_version_dir old` does
- `nvm_normalize_lts`: check for uppercase with a `case` pattern instead of `tr`
- `nvm_alias`: only use a subshell for `lts/-N`, the one form that is rewritten
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9abb216367
commit
3f17b84248
1 file changed
+12
-6
@@ -1300,7 +1300,7 @@ nvm_version_dir() {
|
||||
}
|
||||
|
||||
nvm_alias_path() {
|
||||
nvm_echo "$(nvm_version_dir old)/alias"
|
||||
nvm_echo "${NVM_DIR}/alias"
|
||||
}
|
||||
|
||||
nvm_version_path() {
|
||||
@@ -1718,10 +1718,12 @@ nvm_normalize_lts() {
|
||||
*)
|
||||
case "${LTS}" in
|
||||
lts/*)
|
||||
if [ "${LTS}" != "$(echo "${LTS}" | command tr '[:upper:]' '[:lower:]')" ]; then
|
||||
nvm_err 'LTS names must be lowercase'
|
||||
return 3
|
||||
fi
|
||||
case "${LTS}" in
|
||||
*[[:upper:]]*)
|
||||
nvm_err 'LTS names must be lowercase'
|
||||
return 3
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
nvm_echo "${LTS}"
|
||||
@@ -2170,7 +2172,11 @@ nvm_alias() {
|
||||
nvm_err 'An alias is required.'
|
||||
return 1
|
||||
fi
|
||||
ALIAS="$(nvm_normalize_lts "${ALIAS}")" || return $?
|
||||
# only `lts/-N` is rewritten; anything else is just validated, without a subshell
|
||||
case "${ALIAS}" in
|
||||
lts/-*) ALIAS="$(nvm_normalize_lts "${ALIAS}")" || return $? ;;
|
||||
*) nvm_normalize_lts "${ALIAS}" >/dev/null || return $? ;;
|
||||
esac
|
||||
|
||||
if [ -z "${ALIAS}" ]; then
|
||||
return 2
|
||||
|
||||
Reference in new issue
Block a user