Skip to content

Commit 2ea172e

Browse files
committed
Shortcut to speed up lookup for common scenario
Most #engine version specs just specify a baseline version, which means most likely, the highest installed version will satisfy This does a first pass with just that single version in hopes it satisfies. If so, we can avoid the cost of sh-semver sorting and validating across all the installed versions.
1 parent fc0cd85 commit 2ea172e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

libexec/nodenv-package-json-engine

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,20 @@ find_installed_version_matching_expression() {
5050
installed_versions+=( "$v" )
5151
done < <(nodenv versions --bare --skip-aliases | grep -e '^[[:digit:]]')
5252

53-
version=$("$SEMVER" -r "$version_expression" "${installed_versions[@]}" \
54-
| tail -n 1)
55-
echo "$version"
53+
local fast_guess
54+
fast_guess=$("$SEMVER" -r "$version_expression" "${installed_versions[@]:${#installed_versions[@]}-1}" | tail -n 1)
55+
56+
# Most #engine version specs just specify a baseline version,
57+
# which means most likely, the highest installed version will satisfy
58+
# This does a first pass with just that single version in hopes it satisfies.
59+
# If so, we can avoid the cost of sh-semver sorting and validating across
60+
# all the installed versions.
61+
if [ -n "$fast_guess" ]; then
62+
echo "$fast_guess"
63+
return
64+
fi
65+
66+
"$SEMVER" -r "$version_expression" "${installed_versions[@]}" | tail -n 1
5667
}
5768

5869
get_version_respecting_precedence() {

0 commit comments

Comments
 (0)