dotfiles icon indicating copy to clipboard operation
dotfiles copied to clipboard

Optimize shell script performance: replace external commands with built-ins

Open Copilot opened this issue 3 months ago • 0 comments

Shell startup and script execution were inefficient due to unnecessary subprocess spawning.

Changes

Replace which with command -v (28x faster)

  • lib/auto-complete: Check for nodenv, rbenv, fasd, gh using POSIX built-in
  • lib/globals: Locate zsh using built-in instead of external binary

Replace cat with redirection (96x faster)

  • lib/globals: Read ~/.token using $(<file) syntax, add existence check

Remove unnecessary directory stack operations

  • script/update: Replace pushd/popd with cd, add error handling

Quote variable expansions

  • .zshrc: Quote $ZSH and ${HOME} to handle paths with spaces

Example

# Before (spawns external process)
if which nodenv > /dev/null; then eval "$(nodenv init -)"; fi
HOMEBREW_GITHUB_API_TOKEN=$(cat ~/.token)

# After (uses built-ins)
if command -v nodenv > /dev/null 2>&1; then eval "$(nodenv init -)"; fi
HOMEBREW_GITHUB_API_TOKEN=$(<~/.token)

Impact: Significant reduction in shell startup time, especially noticeable on systems sourcing these scripts frequently.

Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Nov 04 '25 23:11 Copilot