[Perf] nvm_ls: check whether a version directory has entries without forking

Each check ran `ls` piped into `grep` in a subshell,
which cost more than the `find` it guards;
a glob finds the same entries in the shell.
This commit is contained in:
Jordan Harband committed 2026-11-19 16:05:28 -08:00
1 parent 887c2969b7
commit b0a377a524
2 files changed
+54 -4

No files matched your search

+16 -4
View File
@@ -2694,13 +2694,13 @@ nvm_ls() {
NVM_SYSTEM_CHECK='any'
fi
if ! [ -d "${NVM_DIRS_TO_SEARCH1}" ] || ! (command ls -1q "${NVM_DIRS_TO_SEARCH1}" | nvm_grep -q .); then
if ! nvm_dir_has_entries "${NVM_DIRS_TO_SEARCH1}"; then
NVM_DIRS_TO_SEARCH1=''
fi
if ! [ -d "${NVM_DIRS_TO_SEARCH2}" ] || ! (command ls -1q "${NVM_DIRS_TO_SEARCH2}" | nvm_grep -q .); then
if ! nvm_dir_has_entries "${NVM_DIRS_TO_SEARCH2}"; then
NVM_DIRS_TO_SEARCH2="${NVM_DIRS_TO_SEARCH1}"
fi
if ! [ -d "${NVM_DIRS_TO_SEARCH3}" ] || ! (command ls -1q "${NVM_DIRS_TO_SEARCH3}" | nvm_grep -q .); then
if ! nvm_dir_has_entries "${NVM_DIRS_TO_SEARCH3}"; then
NVM_DIRS_TO_SEARCH3="${NVM_DIRS_TO_SEARCH2}"
fi
@@ -4801,6 +4801,18 @@ nvm_write_nvmrc() {
fi
}
# Whether a directory has an entry that `ls` would list, without forking
nvm_dir_has_entries() {
nvm_is_zsh && setopt local_options nonomatch
local NVM_DIR_ENTRY
for NVM_DIR_ENTRY in "${1-}"/*; do
if [ -e "${NVM_DIR_ENTRY}" ] || [ -L "${NVM_DIR_ENTRY}" ]; then
return 0
fi
done
return 1
}
# Check version dir permissions
nvm_check_file_permissions() {
nvm_is_zsh && setopt local_options nonomatch
@@ -7087,7 +7099,7 @@ nvm() {
nvm_needs_lts_status nvm_filter_installed_versions nvm_is_version_selected \
nvm_install_binary nvm_install_source nvm_clang_version \
nvm_get_mirror nvm_get_download_slug nvm_download_artifact nvm_is_cached_artifact \
nvm_install_npm_if_needed nvm_use_if_needed nvm_check_file_permissions \
nvm_install_npm_if_needed nvm_use_if_needed nvm_dir_has_entries nvm_check_file_permissions \
nvm_print_versions nvm_validate_min_version nvm_is_below_min_version nvm_filter_min_versions nvm_compute_checksum \
nvm_get_checksum_binary \
nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \
+38
View File
@@ -0,0 +1,38 @@
#!/bin/sh
cleanup() {
rm -rf "${TEST_DIR}"
}
die() {
echo "$@"
cleanup
exit 1
}
: nvm.sh
\. ../../../nvm.sh
TEST_DIR="$(mktemp -d)"
# glob characters and a space in the path are matched literally
DIR="${TEST_DIR}/a [b] *"
! nvm_dir_has_entries "${DIR}" || die 'a missing directory has no entries'
mkdir "${DIR}"
! nvm_dir_has_entries "${DIR}" || die 'an empty directory has no entries'
touch "${DIR}/.hidden"
! nvm_dir_has_entries "${DIR}" || die 'a directory with only dotfiles has no entries, as with `ls`'
ln -s "${TEST_DIR}/nonexistent" "${DIR}/broken"
nvm_dir_has_entries "${DIR}" || die 'a broken symlink is an entry'
rm "${DIR}/broken"
mkdir "${DIR}/v1.0.0"
nvm_dir_has_entries "${DIR}" || die 'a directory with a subdirectory has entries'
! nvm_dir_has_entries "${TEST_DIR}/a [b] " || die 'the directory name is not a pattern'
cleanup