Optimizing zsh on macos

Every time I open a terminal on macOS, zsh takes like a second to show the prompt. It’s super annoying!

I profiled using

1
2
3
4
5
6
7
8
9
if [ -n "${ZSH_DEBUGRC+1}" ]; then
    zmodload zsh/zprof
fi

# your zsh config

if [ -n "${ZSH_DEBUGRC+1}" ]; then
    zprof
fi

To make it easier I’ve added this alias:

1
alias zperf='time ZSH_DEBUGRC=1 zsh -i -c exit'

Profiling

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
$ zperf
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1          36.73    36.73   26.10%     36.73    36.73   26.10%  autoenv_init
 2)    1          22.16    22.16   15.75%     22.16    22.16   15.75%  _mise_hook
 3)   21          28.72     1.37   20.41%     18.10     0.86   12.86%  _omz_source
 4)    2          15.97     7.99   11.35%     15.97     7.99   11.35%  compaudit
 5)    1          13.75    13.75    9.77%     13.75    13.75    9.77%  (anon) [/Users/mgreco/.oh-my-zsh/tools/check_for_upgrade.sh:157]
 6)    1          24.00    24.00   17.05%     10.24    10.24    7.28%  handle_update
 7)    1           8.93     8.93    6.34%      8.93     8.93    6.34%  zrecompile
 8)    1           8.81     8.81    6.26%      8.81     8.81    6.26%  test-ls-args
 9)    1          19.41    19.41   13.79%      3.44     3.44    2.44%  compinit
10)    1           0.61     0.61    0.43%      0.61     0.61    0.43%  regexp-replace
11)    1           0.57     0.57    0.41%      0.57     0.57    0.41%  colors
12)    6           0.44     0.07    0.31%      0.44     0.07    0.31%  add-zsh-hook
13)    4           0.23     0.06    0.16%      0.23     0.06    0.16%  compdef
14)    2           0.22     0.11    0.16%      0.22     0.11    0.16%  is-at-least
15)    8           0.14     0.02    0.10%      0.14     0.02    0.10%  addToPathFront
16)    8           0.13     0.02    0.10%      0.13     0.02    0.10%  addToPath
17)    1          36.85    36.85   26.18%      0.12     0.12    0.08%  autoenv_cd
18)    4           0.07     0.02    0.05%      0.07     0.02    0.05%  addToMyGitProjects
19)    3           0.03     0.01    0.02%      0.03     0.01    0.02%  is_theme
20)    2           0.01     0.01    0.01%      0.01     0.01    0.01%  env_default
21)    1          36.86    36.86   26.20%      0.01     0.01    0.01%  enable_autoenv
22)    1           0.01     0.01    0.01%      0.01     0.01    0.01%  bashcompinit
23)    1          36.85    36.85   26.19%      0.01     0.01    0.00%  cd

-----------------------------------------------------------------------------------

21)    1          36.86    36.86   26.20%      0.01     0.01    0.01%  enable_autoenv
       1/1        36.85    36.85   26.19%      0.01     0.01             cd [23]

-----------------------------------------------------------------------------------

       1/1        36.85    36.85   26.19%      0.01     0.01             enable_autoenv [21]
23)    1          36.85    36.85   26.19%      0.01     0.01    0.00%  cd
       1/1        36.85    36.85   26.18%      0.12     0.12             autoenv_cd [17]

-----------------------------------------------------------------------------------

       1/1        36.85    36.85   26.18%      0.12     0.12             cd [23]
17)    1          36.85    36.85   26.18%      0.12     0.12    0.08%  autoenv_cd
       1/1        36.73    36.73   26.10%     36.73    36.73             autoenv_init [1]

-----------------------------------------------------------------------------------

       1/1        36.73    36.73   26.10%     36.73    36.73             autoenv_cd [17]
 1)    1          36.73    36.73   26.10%     36.73    36.73   26.10%  autoenv_init

-----------------------------------------------------------------------------------

 3)   21          28.72     1.37   20.41%     18.10     0.86   12.86%  _omz_source
       1/1         0.01     0.01    0.01%      0.01     0.01             bashcompinit [22]
       2/2         0.01     0.01    0.01%      0.01     0.01             env_default [20]
       2/4         0.09     0.04    0.06%      0.09     0.04             compdef [13]
       2/2         0.22     0.11    0.16%      0.22     0.11             is-at-least [14]
       4/6         0.31     0.08    0.22%      0.31     0.08             add-zsh-hook [12]
       1/1         0.57     0.57    0.41%      0.57     0.57             colors[11]
       1/1         0.61     0.61    0.43%      0.61     0.61             regexp-replace [10]
       1/1         8.81     8.81    6.26%      8.81     8.81             test-ls-args [8]

-----------------------------------------------------------------------------------

 6)    1          24.00    24.00   17.05%     10.24    10.24    7.28%  handle_update
       1/1        13.75    13.75    9.77%     13.75    13.75             (anon)[/Users/mgreco/.oh-my-zsh/tools/check_for_upgrade.sh:157] [5]

-----------------------------------------------------------------------------------

 2)    1          22.16    22.16   15.75%     22.16    22.16   15.75%  _mise_hook

-----------------------------------------------------------------------------------

 9)    1          19.41    19.41   13.79%      3.44     3.44    2.44%  compinit
       1/2        15.97    15.97   11.35%      0.18     0.18             compaudit [4]

-----------------------------------------------------------------------------------

       1/2        15.97    15.97   11.35%      0.18     0.18             compinit [9]
       1/2        15.79    15.79   11.22%     15.79    15.79             compaudit [4]
 4)    2          15.97     7.99   11.35%     15.97     7.99   11.35%  compaudit
       1/2        15.79    15.79   11.22%     15.79    15.79             compaudit [4]

-----------------------------------------------------------------------------------

       1/1        13.75    13.75    9.77%     13.75    13.75             handle_update [6]
 5)    1          13.75    13.75    9.77%     13.75    13.75    9.77%  (anon) [/Users/mgreco/.oh-my-zsh/tools/check_for_upgrade.sh:157]

-----------------------------------------------------------------------------------

 7)    1           8.93     8.93    6.34%      8.93     8.93    6.34%  zrecompile

-----------------------------------------------------------------------------------

       1/1         8.81     8.81    6.26%      8.81     8.81             _omz_source [3]
 8)    1           8.81     8.81    6.26%      8.81     8.81    6.26%  test-ls-args

-----------------------------------------------------------------------------------

       1/1         0.61     0.61    0.43%      0.61     0.61             _omz_source [3]
10)    1           0.61     0.61    0.43%      0.61     0.61    0.43%  regexp-replace

-----------------------------------------------------------------------------------

       1/1         0.57     0.57    0.41%      0.57     0.57             _omz_source [3]
11)    1           0.57     0.57    0.41%      0.57     0.57    0.41%  colors

-----------------------------------------------------------------------------------

       4/6         0.31     0.08    0.22%      0.31     0.08             _omz_source [3]
12)    6           0.44     0.07    0.31%      0.44     0.07    0.31%  add-zsh-hook

-----------------------------------------------------------------------------------

       2/4         0.09     0.04    0.06%      0.09     0.04             _omz_source [3]
13)    4           0.23     0.06    0.16%      0.23     0.06    0.16%  compdef

-----------------------------------------------------------------------------------

       2/2         0.22     0.11    0.16%      0.22     0.11             _omz_source [3]
14)    2           0.22     0.11    0.16%      0.22     0.11    0.16%  is-at-least

-----------------------------------------------------------------------------------

15)    8           0.14     0.02    0.10%      0.14     0.02    0.10%  addToPathFront

-----------------------------------------------------------------------------------


18)    4           0.07     0.02    0.05%      0.07     0.02    0.05%  addToMyGitProjects

-----------------------------------------------------------------------------------

19)    3           0.03     0.01    0.02%      0.03     0.01    0.02%  is_theme

-----------------------------------------------------------------------------------

       2/2         0.01     0.01    0.01%      0.01     0.01             _omz_source [3]
20)    2           0.01     0.01    0.01%      0.01     0.01    0.01%  env_default

-----------------------------------------------------------------------------------

       1/1         0.01     0.01    0.01%      0.01     0.01             _omz_source [3]
22)    1           0.01     0.01    0.01%      0.01     0.01    0.01%  bashcompinit

ZSH_DEBUGRC=1 zsh -i -c exit  0.16s user 0.18s system 38% cpu 0.873 total

So, I’ve just disabled:

  • oh-my-zsh
  • completions
  • starship

and look at this now:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$ time ZSH_DEBUGRC=1 zsh -i -c exit
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1          46.78    46.78   69.34%     46.78    46.78   69.34%  autoenv_init
 2)    1          20.14    20.14   29.85%     20.14    20.14   29.85%  _mise_hook
 3)    1          47.00    47.00   69.66%      0.22     0.22    0.32%  autoenv_cd
 4)    8           0.12     0.02    0.19%      0.12     0.02    0.19%  addToPathFront
 5)    8           0.12     0.02    0.18%      0.12     0.02    0.18%  addToPath
 6)    4           0.05     0.01    0.07%      0.05     0.01    0.07%  addToMyGitProjects
 7)    1          47.03    47.03   69.71%      0.02     0.02    0.04%  enable_autoenv
 8)    1          47.01    47.01   69.68%      0.01     0.01    0.01%  cd

-----------------------------------------------------------------------------------

 7)    1          47.03    47.03   69.71%      0.02     0.02    0.04%  enable_autoenv
       1/1        47.01    47.01   69.68%      0.01     0.01             cd [8]

-----------------------------------------------------------------------------------

       1/1        47.01    47.01   69.68%      0.01     0.01             enable_autoenv [7]
 8)    1          47.01    47.01   69.68%      0.01     0.01    0.01%  cd
       1/1        47.00    47.00   69.66%      0.22     0.22             autoenv_cd [3]

-----------------------------------------------------------------------------------

       1/1        47.00    47.00   69.66%      0.22     0.22             cd [8]
 3)    1          47.00    47.00   69.66%      0.22     0.22    0.32%  autoenv_cd
       1/1        46.78    46.78   69.34%     46.78    46.78             autoenv_init [1]

-----------------------------------------------------------------------------------

       1/1        46.78    46.78   69.34%     46.78    46.78             autoenv_cd [3]
 1)    1          46.78    46.78   69.34%     46.78    46.78   69.34%  autoenv_init

-----------------------------------------------------------------------------------

 2)    1          20.14    20.14   29.85%     20.14    20.14   29.85%  _mise_hook

-----------------------------------------------------------------------------------

 4)    8           0.12     0.02    0.19%      0.12     0.02    0.19%  addToPathFront

-----------------------------------------------------------------------------------

 5)    8           0.12     0.02    0.18%      0.12     0.02    0.18%  addToPath

-----------------------------------------------------------------------------------

 6)    4           0.05     0.01    0.07%      0.05     0.01    0.07%  addToMyGitProjects

ZSH_DEBUGRC=1 zsh -i -c exit  0.06s user 0.09s system 30% cpu 0.462 total

I have a terribly boring and plain terminal, but at least it’s very fast. BTW, I’ve never had this issue on Linux. I Hate you MacOS (written on MacOS)

Update 2025-04-07

After some more iterations, I decided to get rid of oh-my-zsh and all my custom functions (addToPath, addToMyGitProjects, …) as well, and now it flies!

$ zperf
num  calls                time                       self            name
-----------------------------------------------------------------------------------
ZSH_DEBUGRC=1 zsh -i -c exit  0.01s user 0.02s system 66% cpu 0.049 total