TLDR: configure lazy loading of nvm
via the updated .zshrc
file at the end of my answer
It was nvm
taking up nearly 88% of my load time.
One of the answers points to this but the solution breaks node
and npm
, making them dependent on nvm
first being executed or you'll get these errors:
/Users/xxx/.nvm/nvm.sh:2833: defining function based on alias `nvm'
/Users/xxx/.nvm/nvm.sh:2833: parse error near `()'
dyld[57650]: Library not loaded: /opt/homebrew/opt/icu4c/lib/libicui18n.73.dylib
Referenced from: <758FD1B7-1836-321E-A1D9-E47EC3C39702> /opt/homebrew/Cellar/node/21.5.0/bin/node
Reason: tried: '/opt/homebrew/opt/icu4c/lib/libicui18n.73.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/icu4c/lib/libicui18n.73.dylib' (no such file), '/opt/homebrew/opt/icu4c/lib/libicui18n.73.dylib' (no such file), '/opt/homebrew/Cellar/icu4c/74.2/lib/libicui18n.73.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/icu4c/74.2/lib/libicui18n.73.dylib' (no such file), '/opt/homebrew/Cellar/icu4c/74.2/lib/libicui18n.73.dylib' (no such file)
Add zmodload zsh/zprof
as the 1st line of your .zshrc
file and zprof
as the last line.
Restart your shell and you'll get something similar to this:
Last login: Sun Mar 31 00:15:38 on ttys000
num calls time self name
-----------------------------------------------------------------------------------
1) 1 442.05 442.05 84.53% 254.54 254.54 48.68% nvm_auto
2) 2 187.51 93.75 35.86% 91.66 45.83 17.53% nvm
3) 1 75.70 75.70 14.48% 64.37 64.37 12.31% nvm_ensure_version_installed
4) 22 25.51 1.16 4.88% 21.05 0.96 4.02% _omz_source
...
Nearly 90% of my loading time is took up by nvm
!
The real way to fix this is by using the nvm
plugin and enabling lazy loading, as opposed to trying to fiddle with the commands that nvm
adds (or prompts you to add - I've forgot which one it is.
This option will help you to defer nvm's load until you use it to speed-up your zsh startup. This will source nvm script only when using it, and will create a function for node, npm, npx, pnpm, yarn, and the command(s) specified by lazy-cmd option, so when you call either of them, nvm will be loaded and run with default version.
Before:
...
plugins=(git)
...
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
/usr/bin/time zsh -i -c exit
0.43 real 0.16 user 0.23 sys
After (note the zstyle
line added, nvm
plugin added next to git
& the export NVM_DIR...
lines deleted):
...
zstyle ':omz:plugins:nvm' lazy yes
plugins=(git nvm)
...
/usr/bin/time zsh -i -c exit
0.14 real 0.06 user 0.06 sys
A ~70% decrease.
compinit
is the cause, Z shell has a profiling module