Debug and Optimize ZSH Loading Time

kinopyo avatar

kinopyo

Benchmark

$ /usr/bin/time zsh -i -c exit
1.40 real         0.19 user         0.17 sys

Whenever I open another tab / pane, my ZSH is perceivably slow. It takes 1.4 seconds to load.

Debug

zsh -xv

This will output screens of verbose messages. Pay attention at where it hangs, that's where things get slow.

(I even recorded the screen with QuickTime so I can go back and inspect... 😅)

Optimize

For me it was the eval "$(rbenv init -)" command that hangs the flow. Removing that and run the benchmark again.

# eval "$(rbenv init -)"

Then it only takes 0.3 seconds to load, 80% boost! 🎉

$ /usr/bin/time zsh -i -c exit
0.30 real         0.10 user         0.07 sys

You can run rbenv init - to see what it does:

export PATH="~/.rbenv/shims:${PATH}"
export RBENV_SHELL=zsh
source '/usr/local/Cellar/rbenv/1.1.2/libexec/../completions/rbenv.zsh'
command rbenv rehash 2>/dev/null
rbenv() {
  local command
  command="${1:-}"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  rehash|shell)
    eval "$(rbenv "sh-$command" "$@")";;
  *)
    command rbenv "$command" "$@";;
  esac
}

I do put the shims to my PATH ,but I don't need the rest like command completions and rehash on loading time.

-eval "$(rbenv init -)"
+export PATH=~/.rbenv/shims:$PATH

Credit goes to https://bennycwong.github.io/post/speeding-up-oh-my-zsh/ 👍.

kinopyo avatar
Written By

kinopyo

Indoor enthusiast, web developer, and former hardcore RTS gamer. #parenting
Enjoyed the post?

Clap to support the author, help others find it, and make your opinion count.