How to debug zsh startup time

My ZSH shell started to be slow, by slow I mean startup time was about ~5sec.

Step 1: measure startup time:

time  zsh -i -c exit

Step 2: Inject profiling code

At the beginning of your .zshrc add following: zmodload zsh/zprof and at the end add: zprof, this will load zprof mod and display what your shell was doing durung your initialization.

After start of the shell you'll see something along the lines:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1          49.02    49.02   26.87%     49.02    49.02   26.87%  a command
 2) ....

Commands ZSH spent most of the time are at the top of the output.

In my case culprit was compinit.

Step 3: Make compinit faster

Compinit is a function that initializes shell completion. After some googling I found out that the problem might be "too big" .zcompdump file, in my case it was about 1mb, so I deleted it (this file is autogenerated), and ZHS magically started to be faster.

After that I have added deleting this file to my user startup script.

Step 4: Cleanups

Comment out or delete additions you made to .zshrc file.