Skip to content
mistergraphxPublic

About

Fast zsh config without a framework: 44 ms startup vs 2.3 s with oh-my-zsh. Lazy-loaded nvm, compiled completion cache, fzf, agnoster.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

36 Commits

Folders and files

Repository files navigation

zsh config: fast startup, no framework

My personal zsh setup for macOS. Same comfort as my old oh-my-zsh setup (theme, fuzzy finder, autosuggestions, syntax highlighting, completions, nvm), a fraction of the startup time.

Shell startup: 2,342 ms → 44 ms, measured with zprof on a MacBook Pro 2015 (macOS 12).

Setup Startup time
oh-my-zsh + fzf, nvm, autosuggestions, syntax highlighting, interactive-cd 2,342 ms
This config, nvm loaded at startup 1,217 ms
This config, nvm lazy-loaded, completion dump not compiled ~375 ms
This config, current version 44 ms (1,316 ms once a day, when the completion cache is rebuilt)

The full measurement log, step by step, is in docs/benchmarks.md.

What made the difference

  1. No framework. Plugins are git submodules, sourced directly. Nothing else is loaded.
  2. nvm is lazy-loaded. Loading nvm at startup cost about 1 second. Now node, npm and nvm are small autoloaded functions that load nvm on first use, then replace themselves. Every other binary in the active Node version's bin directory (npx, corepack, global npm installs) gets the same stub, created at startup by nvm_lazy_stubs. A global install made after the shell starts is covered from the next start.
  3. The completion dump is rebuilt once a day and compiled. compinit re-parsing about 1,000 compdef calls on every start was the next bottleneck. The dump is now regenerated when older than 24 hours, then compiled to bytecode with zcompile. Other starts use compinit -C on the compiled file: about 6 ms instead of 25. Careful when copying the staleness check: the (#q…) glob qualifier in $ZSH_COMPDUMP(#qN.mh+24) is silently ignored unless setopt extendedglob is on.
  4. fzf's shell integration is cached. source <(fzf --zsh) launched the fzf binary on every start for output that only changes with fzf itself. It is now generated once, compiled, and regenerated only when the fzf binary changes.
  5. No needless subprocesses. For example, the cache directory is only created when it doesn't exist yet.

What's included

  • Theme: agnoster (needs a Powerline-compatible font)
  • Plugins: fzf, zsh-autosuggestions, fast-syntax-highlighting, zsh-completions, git completion
  • History: shared between sessions, duplicates removed, 10,000 entries
  • Directories: auto_cd, directory stack (cd -1, cd -2…)
  • Functions (autoloaded, one file per function): mkcd, kill_port, lazy node / npm / nvm, and nvm_lazy_stubs (lazy stubs for the other nvm binaries, run at startup)
  • Aliases: safe rm / cp / mv, and a few personal shortcuts

Install

git clone --recurse-submodules https://github.com/mistergraphx/zsh.git ~/path/to/zsh

Then in ~/.zshrc:

# Set to 1 to print a zprof report and the startup time
export PROFILING_MODE=0

source ~/path/to/zsh/main.zsh

env.zsh contains macOS paths specific to my machine (Homebrew, MacPorts, MAMP): adapt them to yours.

Measure it yourself

Set PROFILING_MODE=1 and open a new terminal: you get the zprof report and the startup time in milliseconds. Run it a few times, since the first start after a change (or once a day) rebuilds the caches.

Structure

main.zsh        entry point: options, theme, plugins, completion, profiling
env.zsh         PATH and environment
aliases.zsh     aliases
functions/      autoloaded functions, one per file
plugins/        git submodules (+ git completion)
themes/         git submodule (agnoster)
.cache/         history, completion dump, fzf cache (generated)

Sources

About

Fast zsh config without a framework: 44 ms startup vs 2.3 s with oh-my-zsh. Lazy-loaded nvm, compiled completion cache, fzf, agnoster.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages