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

Created on 4 Apr 2018  Â·  5Comments  Â·  Source: nvm-sh/nvm

  • 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!)
performance

Most helpful comment

@andybergon please try this solution https://github.com/creationix/nvm/issues/539#issuecomment-403661578

# Install zsh-async if it’s not present
if [[ ! -a ~/.zsh-async ]]; then
    git clone [email protected]: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

All 5 comments

Duplicate of #539 and #1242.
Workaround here

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

@andybergon please try this solution https://github.com/creationix/nvm/issues/539#issuecomment-403661578

# Install zsh-async if it’s not present
if [[ ! -a ~/.zsh-async ]]; then
    git clone [email protected]: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

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 "$@"
}

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

I am loathe to mention the competition (sorry), but nvs doesn't seem to have these delays, and also has a neat feature 'nvs link' where it will setup a 'default' node in your path (by symlink magic) that can afford any process in your system with a workable node/npm environment even in shells/processes that know nothing about nvs.

I think it would be very cool if npm also had this feature. NVS copied a lot of NVM. Time to return the favour perhaps!

Was this page helpful?
0 / 5 - 0 ratings