Zsh Startup Acceleration
Zsh is already the default shell of macOS Catalina. As we configure more and more plugins for zsh, the startup speed of zsh becomes slower and slower. This article will introduce how to measure and optimize the startup speed of Zsh.
Mesure
We can use the time
command to view the startup time of Zsh in a coarse-grained manner
Before optimization:
1 | $ time zsh -i -c exit |
After optimization(with nvm):
1 | $ time zsh -i -c exit |
After optimization(without nvm):
1 | Saving session...completed. |
Troubleshoot
Use zprof
to see the time-consuming of Zsh loading. Edit the ~/.zshrc
file and add the following code at the beginning.
1 | zmodload zsh/zprof |
Start zsh again, and enter zprof
, you can see that it lists specific time-consuming items.
1 | $ zprof |
Optimization
use nvm
The nvm(Node Version Manager) is a well-known node.js
version management tool. Take zsh as an example. After installing nvm, the following code is usually added to .zshrc
.
1 | export NVM_DIR="$HOME/.nvm" |
But this item will seriously slow down the startup speed of zsh. We can change it to the following code.
1 | export NVM_DIR="$HOME/.nvm" |
But the solution above will ignore the packages which we install on system, for example, if you use npm install -g hexo
, the hexo
will not work until you use npm
again manually.
So, it’s better to use the solution below, which provided from this link.
1 | export NVM_DIR="$HOME/.nvm" |
use n
When we use the above solution, the commands installed through NPM cannot be used directly. So let’s use another Node version manager to replace NVM, which is n.
N is really easy to install, just use brew.
1 | brew install n |
And remove your NVM directories.
Put these lines on your .zshrc
.
1 | # For node.js |