So recently I noticed the initialisation lag when starting a new zsh session. I decided to fix it and here's what I've learnt.
First of all, credits to this article, which provides a handy way to profile zsh startup stage. Follow the guide in there to profile first, and read on.
How to profile your zsh startup time
My tips #
- See what parts of the initalisation stage takes so much time from the above profile results. Remove or lazy-load them.
- Give Prezto a try. It can sometimes be faster than Oh My ZSH.
- Pick a fast plugin manager. I choose Antibody over its performance and simplicity.
- Lazy load package managers or other stuff that can be lazy-loaded. npm is one example.
- powerlevel10k is a very powerful and customisable zsh theme, that not only changes how the shell look but enhances the performance dramatically. You should definitely give it a try!
- Remove unused zsh plugins.
- Replace
brew --prefix
with a hardcoded value. - Remove thefuck, if you haven't, from your
.zshrc
. - Set
ZSH_DISABLE_COMPFIX="true"
. I don't really need the security check and it makes zsh load faster. - iTerm2 Preferences > Profiles > default profile > General > Command > choose "command" and use
/bin/zsh
. This skips macOS shell login logging.
That's it. There are plenty of resources online of the above for you to search, not gonna cover all of 'em here! Sorry 😜. This is what I've achieved:
$ for i in $(seq 1 10); do /usr/bin/time zsh -i -c exit; done
0.36 real 0.27 user 0.10 sys
0.36 real 0.27 user 0.10 sys
0.35 real 0.27 user 0.10 sys
0.36 real 0.28 user 0.11 sys
0.36 real 0.27 user 0.11 sys
0.35 real 0.27 user 0.10 sys
0.37 real 0.28 user 0.11 sys
0.37 real 0.28 user 0.11 sys
0.40 real 0.29 user 0.12 sys
0.51 real 0.36 user 0.15 sys
You can check out my dotfiles for some inspirations.