2023-02-24

Speed up zsh startup time

Terminal startup became noticeably slow. It took me like 1-2 seconds for prompt to show up.

This is for zsh users.

  • Profiling zsh scripts

.zshrc

# Add below on top of the .zshrc
zmodload zsh/zprof
# Run profiler
 zprof
  • You can see what's being run by running below line

/bin/zsh -i -x
  • Check the time it takes to run zsh scripts

/usr/bin/time -p /bin/zsh -i -c exit

In my case, nvm (Node Version Manager) was the culprit.

How to fix slow zsh because of nvm

  1. Comment out scripts generated by nvm
  2. Use nvm from ohmyzsh plugins
  3. Defer nvm loading (lazy startup)
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
 
zstyle ':omz:plugins:nvm' lazy yes
 
plugins=(
...
nvm
)

Search showed different tactics to speed up on the stackoverflow. However, none of them made any difference in my case.