Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

nvm.sh and bash_completion add 2 seconds to shell load time #1774

Closed
andybergon opened this issue Apr 4, 2018 · 5 comments · Fixed by #2317
Closed

nvm.sh and bash_completion add 2 seconds to shell load time #1774

andybergon opened this issue Apr 4, 2018 · 5 comments · Fixed by #2317
Labels
performance This relates to anything regarding the speed of using nvm.

Comments

@andybergon
Copy link

  • Operating system and version:
    macOS Sierra 10.12.6

  • nvm debug output:
    I don't think it is relevant for this issue but here it is.

nvm --version: v0.33.8
$TERM_PROGRAM: iTerm.app
$SHELL: /bin/zsh
$HOME: /Users/abergonzo
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.2 (x86_64-apple-darwin16.0)'
uname -a: 'Darwin 16.7.0 Darwin Kernel Version 16.7.0: Tue Jan 30 11:27:06 PST 2018; root:xnu-3789.73.11~1/RELEASE_X86_64 x86_64'
OS version: Mac 10.12.6 16G1314
curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin16.0) libcurl/7.54.0 SecureTransport zlib/1.2.8
wget: /usr/local/bin/wget, GNU Wget 1.19.4 built on darwin16.7.0.
git: /usr/bin/git, git version 2.14.3 (Apple Git-98)
grep: grep: aliased to grep  --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}), grep (BSD grep) 2.5.1-FreeBSD
awk: /usr/bin/awk, awk version 20070501
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
sed: /usr/bin/sed,
cut: illegal option -- -
usage: cut -b list [-n] [file ...]
       cut -c list [file ...]
       cut -f list [-s] [-d delim] [file ...]
cut: /usr/bin/cut,
basename: illegal option -- -
usage: basename string [suffix]
       basename [-a] [-s suffix] string [...]
basename: /usr/bin/basename,
rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
rm: /bin/rm,
mkdir: illegal option -- -
usage: mkdir [-pv] [-m mode] directory ...
mkdir: /bin/mkdir,
xargs: illegal option -- -
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
             [-L number] [-n number [-x]] [-P maxprocs] [-s size]
             [utility [argument ...]]
xargs: /usr/bin/xargs,
nvm current: v9.10.0
which node: $NVM_DIR/versions/node/v9.10.0/bin/node
which iojs: iojs not found
which npm: $NVM_DIR/versions/node/v9.10.0/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v9.10.0
npm root -g: $NVM_DIR/versions/node/v9.10.0/lib/node_modules
  • nvm ls output:
    I don't think it is relevant for this issue but here it is.
        v8.11.0
->      v9.10.0
         system
default -> node (-> v9.10.0)
node -> stable (-> v9.10.0) (default)
stable -> 9.10 (-> v9.10.0) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> v8.11.0)
lts/argon -> v4.9.0 (-> N/A)
lts/boron -> v6.14.0 (-> N/A)
lts/carbon -> v8.11.0
  • How did you install nvm? (e.g. install script in readme, Homebrew):
    Install script in README. (I thought brew installation was not supported as stated in the docs)

  • What steps did you perform?
    I added the following to my .bashrc file.

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" 

(I know bash_completion will probably work only with bash and I know I can install nvm as a zsh plugin but I change shell sometimes)

  • What happened?
    With those 2 lines enabled:
[abergonzo:~]$ /usr/bin/time zsh -i -c exit
        2.45 real         1.08 user         0.97 sys

Without those 2 lines:

[abergonzo:~]$ /usr/bin/time zsh -i -c exit
        0.43 real         0.20 user         0.14 sys

So the startup of a shell (zsh in my case) was slowed down by ~2 seconds adding those 2 lines.

  • What did you expect to happen?
    I expected the loading to be faster or at least that the time it adds to be in the docs as it is significant.
    (Thanks for your work!)
@andybergon
Copy link
Author

Duplicate of #539 and #1242.
Workaround here

Probably worth to add to the docs as I am not the first one? :)

@ljharb ljharb added the performance This relates to anything regarding the speed of using nvm. label Apr 10, 2018
@belozer
Copy link

belozer commented Jul 10, 2018

@andybergon please try this solution #539 (comment)

# Install zsh-async if it’s not present
if [[ ! -a ~/.zsh-async ]]; then
    git clone git@github.com:mafredri/zsh-async.git ~/.zsh-async
fi
source ~/.zsh-async/async.zsh

export NVM_DIR="$HOME/.nvm"
function load_nvm() {
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
    [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
}

# Initialize a new worker
async_start_worker nvm_worker -n
async_register_callback nvm_worker load_nvm
async_job nvm_worker sleep 0.1

@well1791
Copy link

well1791 commented Oct 30, 2018

how about this solution?

nvm() {
    unset -f nvm
    export NVM_DIR=~/.nvm
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    nvm "$@"
}

node() {
    unset -f node
    export NVM_DIR=~/.nvm
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    node "$@"
}

npm() {
    unset -f npm
    export NVM_DIR=~/.nvm
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    npm "$@"
}

@ljharb
Copy link
Member

ljharb commented Oct 30, 2018

Those don’t allow your globally installed node modules to work prior to first invoking nvm, npm, or node.

@peternann

This comment has been minimized.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
performance This relates to anything regarding the speed of using nvm.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants