lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Performance for `npm` could be even more faster?

Open dalisoft opened this issue 2 months ago • 4 comments

:zap: Summary

Hi @evilmartians team/contributors

Tuning npm package could lead into performance improvements and reducing startup execution.

Opened similar PR/Issue on other projects but impossible to check locally due of hard running local or lacking of running locally easier script.

  • https://github.com/dprint/dprint/pull/839/files & https://github.com/dprint/dprint/pull/840/files
  • https://github.com/biomejs/biome/pull/2359/files
  • https://github.com/oxc-project/oxc/pull/2920/files

As these PR not merged into neither of them, so first decided to ask from you

Value

Command Mean [ms] Min [ms] Max [ms] Relative
../lint-staged.sh 154.3 ± 2.8 151.1 159.3 1.00
lint-staged 306.9 ± 6.1 300.3 320.8 1.99 ± 0.05
lefthook run pre-commit 202.8 ± 4.4 197.3 210.1 1.31 ± 0.04
./lefthook run pre-commit 162.7 ± 3.2 160.4 170.7 1.05 ± 0.03

Behavior and configuration changes

If npx is not important, can be tuned for using lefthook binary from optionalDependencies or prepare script can download and change binary for main npm package bin field.

Test script locally for you

postinstall.js
import { prepare, maps } from "binary2npm";

maps.os = {
  linux: "Linux",
  freebsd: "Freebsd",
  darwin: "MacOS",
  win32: "Windows",
};
maps.arch = {
  arm64: "arm64",
  x64: "x86_64",
  x86: "x86_64",
};

await prepare({
  remote: "github",
  author: "evilmartians",
  repository: "lefthook",
  remoteToken: process.env.GITHUB_TOKEN,
  binary: "lefthook",
  orders: ["binary", "version", "os", "arch"],
});

  • Try run node postinstall.js
  • Then benchmark direct running versus using custom bin.js from npm package

dalisoft avatar Apr 13 '24 02:04 dalisoft

Hey! Lefthook actually implements different ways of distribution. We have lefthook package that utilized optionalDepdencies, we have @evilmartians/lefthook-installer which downloads the binary in a postinstall step and we have @evilmartians/lefthook which simply contains all the binaries.

So, what is your suggestion for improvement? And what is the problem with current npm packages you see?

mrexox avatar Apr 15 '24 07:04 mrexox

I looked into other ways of distribution and it is actually 30% slower than suggested improvements.

I am suggesting using direct binary for package.json like {"bin":"lefthook.exe"} or {"bin":"lefthook"} so no additional overhead when executing lefthook from CLI Currently no problem but only 30% performance loss and after improvement CLI speed for npm could be improved by 30%

dalisoft avatar Apr 15 '24 08:04 dalisoft

Oh, I see. Ok, I agree, it would be great to have this improvement.

mrexox avatar Apr 15 '24 08:04 mrexox

@mrexox See #705 please

dalisoft avatar Apr 15 '24 08:04 dalisoft