-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nvm.sh is slow (200ms+) #539
Comments
#337 would alleviate this by requiring much less code to be sourced. If someone were to profile the script and figure out which parts were slowest, I'd be happy to try to speed them up. PRs are also welcome :-) |
@ljharb My apologies -- I just realized that RVM takes just about as long as NVM when sourcing it for the first time. It exports a |
In zsh I use lazy loading for source files.
|
not a bad idea. It could even be more specific as in: nvm() { . "$NVM_DIR/nvm.sh" ; nvm $@ ; } However, that wouldn't detect |
Same here... still looking for a solution..
|
This is pretty slow:
This line is about 0.03s and gets called twice:
This line is also about 0.03s:
|
Any time However, I think the risks of people unknowingly having a |
Thanks, @ljharb. The delay was more of an observation than an annoyance. |
$ time source ~/.nvm/nvm.sh
real 0m3.163s
user 0m1.504s
sys 0m1.172s clean install of nvm, no .nvmrc. what could be the reason? |
@dreyks Likely because |
@ljharb this still leaves about 2.5 seconds for the rest $ time npm config get prefix
/home/user/.nvm/versions/node/v6.3.1
real 0m0.753s
user 0m0.564s
sys 0m0.236s this isn't super critical, cause it happens only when I open another shell, but still I wonder if my setup is wrong |
@dreyks if you try |
$ nvm unalias default
Deleted alias default - restore it with `nvm alias "default" "node"`
$ time source ~/.nvm/nvm.sh
real 0m0.123s
user 0m0.052s
sys 0m0.048s |
When I have either a default alias, or an I think the best solution is to add |
(Obviously these times are specific to my machine, but the relative sizes are what's important). In other words, when no default/nvmrc is defined, it can skip |
the |
For anyone who comes across this in the future;
# nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" --no-use # This loads nvm
alias node='unalias node ; unalias npm ; nvm use default ; node $@'
alias npm='unalias node ; unalias npm ; nvm use default ; npm $@'
|
@parasyte as has been said in this thread and others, add |
@ljharb That's all fine and good, but really inconvenient if you still want to use node. |
@parasyte understood; and your alias is fine too - i'm mostly pointing out that you don't need to remove your default alias, and you can do |
Gotcha. That is a nice little enhancement. I misunderstood that you were providing a tip. 😃 I guess I'll have to keep the alias, and stay hopeful that one day the startup time will get better! Cheers. |
I feel like I must have been doing something wrong, however, to get around some circular logic entering an infinite loop I had to make the unalias the first step.
Thanks |
Also, if I don't have an .nvmrc and I run 'node' then I get a long message from nvm. Is there a best practice to suppress that? |
|
Here's my data point (Macbook Pro, OS X 10.11.6):
This does not change based on whether I source it or run it. |
@ns-cweber why is nvm.sh in /usr/local/opt? did you perhaps install it with homebrew? |
@ljharb That's correct. |
@ns-cweber homebrew installation of nvm is unsupported, and often runs much slower than a normal installation (curling the install script in the readme). Can you try brew uninstalling it, and installing it the recommended way? |
I uninstalled the brew version and reinstalled via
|
Thanks, that's definitely correct. What does |
|
That also looks normal :-/ I'm quite sure you can add |
You're right:
Unless I'm doing something wrong, varying the version of Node doesn't significantly impact performance. |
How about this?
What could be missing with this code? |
@medisoft export PATH="$(nvm_prepend_path "$PATH" "$(nvm_version_path "$(nvm_alias default)")/bin")" That still fails to set the MANPATH but that's pretty close. |
@parasyte 👍 nice workaround here #539 (comment), definitely saves a load of time. Only small annoyance is in the scenario where you start a new session and then want to run a node package cli, i.e. gulp, and you can't cause it's not in the |
For people who want to respect the export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" --no-use # This setups nvm to be lazy-loaded
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
# Alias node, npm, and yarn to access node lazily. We reset the alias on cd to ensure we always use
# the right nvm version when in a new folder (respecting the nvmrc if possible).
alias node='unalias node ; unalias npm ; unalias yarn ; export NVM_LOADED_FOR_PATH=1 ; load-nvmrc ; node $@';
alias npm='unalias node ; unalias npm ; unalias yarn ; export NVM_LOADED_FOR_PATH=1 ; load-nvmrc ; npm $@';
alias yarn='unalias node ; unalias npm ; unalias yarn ; export NVM_LOADED_FOR_PATH=1 ; load-nvmrc ; yarn $@';
unset_nvm() {
if [[ "$NVM_LOADED_FOR_PATH" -eq 1 ]]; then
export NVM_LOADED_FOR_PATH=0
alias node='unalias node ; unalias npm ; unalias yarn ; export NVM_LOADED_FOR_PATH=1 ; load-nvmrc ; node $@';
alias npm='unalias node ; unalias npm ; unalias yarn ; export NVM_LOADED_FOR_PATH=1 ; load-nvmrc ; npm $@';
alias yarn='unalias node ; unalias npm ; unalias yarn ; export NVM_LOADED_FOR_PATH=1 ; load-nvmrc ; yarn $@';
fi
}
add-zsh-hook chpwd unset_nvm The nice thing is that this allows the system node and yarn to still work (as other home-brew dependencies often install them). It works by resetting the alias on |
Note that this still does not allow globally-installed packages in your desired node version to work. |
For that and other reasons, I gave up and switched to managing node with brew. It's ugly, but a few aliases will take care of that, surely. jay@JayMBP:~$ readlink $(which node)
../Cellar/node@6/6.11.3/bin/node
jay@JayMBP:~$ node --version
v6.11.3
jay@JayMBP:~$ brew unlink node@6 && brew link --force node@4
Unlinking /usr/local/Cellar/node@6/6.11.3... 7 symlinks removed
Linking /usr/local/Cellar/node@4/4.8.4_1... 7 symlinks created
If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/node@4/bin:$PATH"' >> ~/.bash_profile
jay@JayMBP:~$ readlink $(which node)
../Cellar/node@4/4.8.4_1/bin/node
jay@JayMBP:~$ node --version
v4.8.4
jay@JayMBP:~$ brew unlink node@4 && brew link --force node
Unlinking /usr/local/Cellar/node@4/4.8.4_1... 7 symlinks removed
Linking /usr/local/Cellar/node/8.6.0... 7 symlinks created
jay@JayMBP:~$ readlink $(which node)
../Cellar/node/8.6.0/bin/node
jay@JayMBP:~$ node --version
v8.6.0
jay@JayMBP:~$ brew unlink node && brew link --force node@6
Unlinking /usr/local/Cellar/node/8.6.0... 7 symlinks removed
Linking /usr/local/Cellar/node@6/6.11.3... 7 symlinks created
If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/node@6/bin:$PATH"' >> ~/.bash_profile
jay@JayMBP:~$ readlink $(which node)
../Cellar/node@6/6.11.3/bin/node
jay@JayMBP:~$ node --version
v6.11.3 |
Maybe need use zsh-async?
|
That wouldn't work for all the other non-zsh shells that nvm supports. |
Yes, it is not a box solution. But for local fix it is convenient, because global modules work. |
@belozer solution is by far the fastest one on my machine |
@belozer - that is what I needed, after a lot of searching. I was using a zsh nvm plugin (tried a couple of different ones), but your solution is the fastest on OSX & Linux. |
* nvm sourcing with brew installed version is slow and bottlenecks shell creation performance * see nvm-sh/nvm#539
use `mafredri/zsh-async` to accelerate the nvm plugin, now works faster nvm-sh/nvm#539 (comment)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Nvm was the slowest part of my prompt. Opening a new shell is super quick now. Reference: nvm-sh/nvm#539 (comment)
On my system (OSX 10.9.5 & bash 4.3.27), nvm adds a lot of time to my .bash_profile:
--This is much slower than rvm--:
I do not use .nvmrc files.
The text was updated successfully, but these errors were encountered: