[Refactor] Use `awk` to improve version comparison performance
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jordan Harband · committed by Jordan Harband · parent 4842641 · Browse files · 2 files·+29 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+29 added−15 removed
- tests:+4 added−1 removed
- other:+25 added−14 removed
Provenance
- Commit
8a138d9- Tree
a113305- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
4842641
2 files changed+29 added−15 removed
modified:test/fast/Unit tests/nvm_version_path+4 added-1 removed
2 unmodified lines3 die () { echo $@ ; exit 1; }4 5 . ../../../nvm.sh6 7-[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] || die '"nvm_version_path foo" did not return correct location'7+OUTPUT="$(nvm_version_path foo)"8+EXPECTED_OUTPUT="$NVM_DIR/foo"9+[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_version_path foo' did not return correct location; expected '$EXPECTED_OUTPUT', got '$OUTPUT'"10+11 [ "$(nvm_version_path 2>&1)" = "version is required" ] || die '"nvm_version_path" did not error out'12 [ "$(nvm_version_path v0.11.0)" = "$NVM_DIR/v0.11.0" ] || die 'old version has the wrong path'13 [ "$(nvm_version_path v0.12.0)" = "$NVM_DIR/versions/node/v0.12.0" ] || die 'new version has the wrong path'14 [ "$(nvm_version_path iojs-v0.12.0)" = "$NVM_DIR/versions/io.js/v0.12.0" ] || die 'iojs version has the wrong path'1 unmodified linemodified:nvm.sh+25 added-14 removed
164 unmodified lines165 fi166 }167 168 nvm_version_greater() {169- local LHS170- LHS="$(nvm_normalize_version "$1")"171- local RHS172- RHS="$(nvm_normalize_version "$2")"173- [ "$LHS" -gt "$RHS" ];169+ command awk 'BEGIN {170+ if (ARGV[1] == "" || ARGV[2] == "") exit(1)171+ split(ARGV[1], a, /\./);172+ split(ARGV[2], b, /\./);173+ for (i=1; i<=3; i++) {174+ if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);175+ if (b[i] && b[i] !~ /^[0-9]+$/) { exit(0); }176+ if (a[i] < b[i]) exit(3);177+ else if (a[i] > b[i]) exit(0);178+ }179+ exit(4)180+ }' "${1#v}" "${2#v}";181 }182 183 nvm_version_greater_than_or_equal_to() {177- local LHS178- LHS="$(nvm_normalize_version "$1")"179- local RHS180- RHS="$(nvm_normalize_version "$2")"181- [ "$LHS" -ge "$RHS" ];184+ command awk 'BEGIN {185+ if (ARGV[1] == "" || ARGV[2] == "") exit(1)186+ split(ARGV[1], a, /\./);187+ split(ARGV[2], b, /\./);188+ for (i=1; i<=3; i++) {189+ if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);190+ if (a[i] < b[i]) exit(3);191+ else if (a[i] > b[i]) exit(0);192+ }193+ exit(0)194+ }' "${1#v}" "${2#v}";195 }196 197 nvm_version_dir() {198 local NVM_WHICH_DIR144 unmodified lines343 ;;344 *)345 local VERSION346 VERSION="$(nvm_strip_iojs_prefix "$1")"334- nvm_version_greater "$VERSION"347+ nvm_version_greater_than_or_equal_to "$VERSION" 0348 ;;349 esac350 }351 58 unmodified lines410 }411 412 nvm_binary_available() {413 # binaries started with node 0.8.6401- local FIRST_VERSION_WITH_BINARY402- FIRST_VERSION_WITH_BINARY="0.8.6"403- nvm_version_greater_than_or_equal_to "$(nvm_strip_iojs_prefix "$1")" "$FIRST_VERSION_WITH_BINARY"414+ nvm_version_greater_than_or_equal_to "$(nvm_strip_iojs_prefix "${1-}")" v0.8.6415 }416 417 nvm_print_formatted_alias() {418 local ALIAS2161 unmodified lines