toddbluhm / env-cmd

Setting environment variables from a file
https://www.npmjs.com/package/env-cmd
MIT License
1.73k stars 65 forks source link

Slow: ~300ms startup penalty #349

Open vjpr opened 2 years ago

vjpr commented 2 years ago

On the latest M1 Mac:

time npm exec env-cmd node index.js

npm exec env-cmd node index.js  0.24s user 0.07s system 95% cpu 0.320 total

320ms is a lot.

a. slim down

I wonder if it can be slimmed down? A simple Node invocation takes 40ms for comparison.

time node -e "console.log('hi');"

node -e "require('http'); console.log('hi');"  0.03s user 0.01s system 94% cpu 0.040 total

The following script replicates the behavior of env-cmd and weighs 73ms which seems like the theoretical minimum startup time.

const cp = require('child_process')
const fs = require('fs')
const env = fs.readFileSync('.env', 'utf8')
console.log(env)
cp.spawnSync('node', ['./main.js'])

b. use rust Another option is migrating to Rust I guess if the logic is too heavy. E.g. This project replaces npm with a Rust version -> https://github.com/egoist/dum

KROSF commented 2 years ago

@vjpr if you still need env-cmd try muchobien/env-cmd is built with go so is much faster

R-Bower commented 1 year ago

@vjpr if you still need env-cmd try muchobien/env-cmd is built with go so is much faster

It's incomplete. Doesn't support all of the options that this library does. See: https://github.com/muchobien/env-cmd/issues/18