gera2ld.space

Why I Use Bun for Automation Scripts


As you all know, JavaScript is the best language in the world, because almost everyone uses it, literally. TypeScript makes it much easier to create less error-prone JavaScript.

I have a lot of automation scripts to make life easier, for example, data backup, email handling, cryptocurrency trading, etc.

At the beginning I made really simple Bash scripts. Soon after that I realized that I wanted more.

So I started writing scripts in TypeScript.

Node.js

Node.js was the most natural runtime for JavaScript.

Here are the takeaways from my experiences:

  • Pros
    • Great ecosystem
    • Cross-platform support, including Raspberry Pi
  • Cons
    • No built-in TypeScript support
    • Lots of configuration files
    • Tons of preparation work to run a dead simple TypeScript script
    • Bad C++ Addon compatibility. Compilation is required for almost each Node.js version, e.g. SQLite3

It is really a pain to create a simple script using Node.js. We have to include quite a few configuration files in the project root before we can start writing TypeScript.

Config files

Deno

Deno is a good alternative to Node.js with native TypeScript support. But it works differently from Node.js, which makes it harder to replace Node.js.

Here are my takeaways:

  • Pros
    • Built-in TypeScript support
    • Compatible with Node.js to some extent
    • Run a script by URL, import a dependency by URL
    • No extra installation needed, no infamous node_modules, simply host the script somewhere on the Internet and run it with its URL
  • Cons
    • No official arm64 (Raspberry Pi) support
    • Frequent API change
    • No easy way to manage the dependencies of a library due to the lack of a centralized place to maintain them

Deno import

Speaking of the dependency management, I know we have importmap, but it only works in the application rather than the dependencies themselves.

If we want to build a library for a Deno project, there is no easy way to maintain its dependencies, which is why I later found many projects written in Node.js and then built for Deno by converting the dependency paths into URLs at compile time.

Despite the ecosystem issue, Deno powered Deno Deploy is a great platform for serverless projects. I can get started with any script from the Internet. Amazing!

Bun

Bun came out later as a Node.js compatible package manager and JavaScript runtime, and it’s fast!

I found it a good choice for running automation scripts. Here are my takeaways:

  • Pros
    • Built-in TypeScript support
    • Compatible with Node.js to some extent
    • Cross-platform support, including Raspberry Pi
    • Super fast
    • Built-in SQLite support
    • Nice APIs for common functions, much easier to use compared to its alternatives, e.g. reading stdin, running a shell command
  • Cons
    • Not so stable according to the community, though it works well for my automation scripts

Bun

Besides the nice APIs such as reading stdin, Bun kept adding new features to make it an easy-to-use runtime.

The recently added $ (Bun Shell) is so convenient that it brings the concision of Bash to TypeScript, which is another reason why I should use it for my scripts.

Verdict

Each runtime has its trade-off.

Finally I migrated from Deno to Bun for its easier dependency management, Raspberry Pi support, and nice APIs for automation scripts.