Why is zsh slow to start?
Problem
After adapting oh-my-zsh
to zsh
, it was found that zsh
takes some time to start up. By executing \time zsh -i -c exit
to check the zsh
startup time, the output is as follows:
~ \time zsh -i -c exit
3.79 real 0.17 user 0.18 sys
It can be seen that loading the zsh
module takes 3.79s
, which can be particularly inconvenient for development.
Optimization Methods
zprof
is a built-in performance monitoring tool in zsh
that can quickly detect performance overhead during zsh
runtime. To use it, add the zmodload zsh/zprof
command at the top of the ~/.zshrc
module and then re-execute zshrc
(restart the console). In the newly opened console, enter the zprof
command to see the following output:
num calls time self name
-----------------------------------------------------------------------------------
1) 1 3394.58 3394.58 78.49% 3394.58 3394.58 78.49% is_update_available
2) 2 656.08 328.04 15.17% 654.53 327.26 15.13% bracketed-paste-magic
3) 1 218.95 218.95 5.06% 82.34 82.34 1.90% nvm_auto
4) 2 136.61 68.31 3.16% 70.00 35.00 1.62% nvmss
-----------------------------------------------------------------------------------
It can be observed that the is_update_available
item takes up 78.49%
of the loading time. This means that when running oh-my-zsh
, it checks for updates. Therefore, you can disable the automatic update check of oh-my-zsh
before executing the source $ZSH/oh-my-zsh.sh
command in the .zshrc
module.
There are two implementation methods to disable automatic update checking
Not recommended method (environment variable in
.zshrc
)bashDISABLE_AUTO_UPDATE=true
Recommended method (
zstyle
setting)bashzstyle ':omz:update' mode disabled
After restarting zsh
, the running time is as follows:
~ \time zsh -i -c exit
0.34 real 0.14 user 0.16 sys
It can be seen that it runs smoothly now.