My terminal is procrastinating πŸ™„

omrilotan
3 min readSep 15, 2019

Reduce terminal instance boot time

I am a performance and observability enthusiast. I am also extremely impatient. This combination of traits drives me to find ways to accelerate any process that takes even a tiny bit longer than absolute necessary.

I made my terminal boot procrastinate for efficiency!

I want my terminal to perform operations only when they are required. I already measure everything that my bashrc runs. So I picked the scripts from my bashrc who take the longest to run, and turned them into on-demand commands.

thefuck

This one was easy.

thefuck is brilliant in correcting your previous console command, but the setup takes me nearly 700ms on average. Unthinkable!

From thefuck documentation

Instead of setting it up up front when terminal instance boots up β€” I prefer to set it up when I need it. So I set up this idempotent function:

function fuck {
eval "$(thefuck --alias)"
eval "fuck $@"
}

First time I call fuck in an instance it performs the following operations:

  1. Setup thefuck with fuck alias (overrides this function).
  2. Trigger fuck with all the passed in arguments.

So initial setup time is reduced to 20ms and usage gets the hit of 700ms later, on demand. Second call will not take that hit.

nvm

Now, nvm I use more often β€” but it cost me over a whopping 1.5 seconds. It also is a bit trickier β€” because it loads up an environment, not just a command.

I chose to add this idem potency to the four major commands I use: node, npm, npx, nvm - but you can easily add more.

Each one of the commands is set up to:

  1. Unset all commands in the list (plus the nvm_load command)
  2. Load up NVM
  3. Call the original command with all arguments

The draw back is that globally installed NPM packages won’t work until you explicitly call one of these method names. On the other hand β€” you can add anything to the list if there’s a command you use often and want to join the party ( yarn etc).

There you go, NVM reduced to 80ms and will only take up your time when needed.

Now that NVM is not included by default, we also need to set it up when entering a directory that contains a .nvmrc file.

cd() {
dir=$@
: ${dir:=~}

builtin cd $dir
if [ -f ".nvmrc" ]; then
nvm use
fi
}

What do you think? Anything slowing you down loading up a terminal instance?

Originally published at https://dev.to on September 15, 2019.

--

--