Errol Hassall

View Original

First impressions of Bun

Bun arrived on the scene in 2023, it was something I had been meaning to check out but never got around too. Early 2024, I was looking into a few Node API constructions, frameworks such as Express, and then I heard about Hono. This is an Express like framework to create APIs using JavaScript. What’s really cool, other than the speed, is that it supports other runtimes such as Bun and Deno. This gave me the idea to give Bun a go along with Hono.

It turns out that they’re an amazing pair. First off it just works, Bun have made the smart decision to have as close to a 1:1 parody with existing Node APIs. This means that you can swap things often with zero effort. The only real caveat to this is that there are some strange behaviours with the occasional packages.

For example, when I was using Bcrypt a common way to hash passwords, the inbuilt Bun package manager, (which is stupid fast by the way) for some reason couldn’t handle the Bcrypt dev dependencies properly as seen here. Now this was weird, and I spent way too long on this only to realise that Bun has its own crypto functions, theirs are based on the popular Argon2. Awesome, I can just remove Bcrypt and use the inbuilt and all my issues are resolved. It is still worth noting that you might find peculiar issues like this, surly I was doing something wrong, but I couldn’t work it out.

Selling points

Development experience

Hono was a dream, but so was Bun. It has first class Typescript support, which means you don’t have to install anything fancy, or preform any bundling to have Typescript in your API, you just need a `tsconfig` file and a `.ts` file, Bun handles the rest. Furthermore, it can bundle your code, it’s a drop-in replacement for NPM or Yarn. It supports JSX, it has a watch mode for your code. It has just about everything you need, the only thing you are missing at this point is a formatter, hopefully this will be added one day. I did find a faster alternative to Prettier, named Biome.

Speed

The speed, everything from the package manager to the compile and run times, it’s fast. Everything you do is simply fast, much faster than Node could ever hope to be.

1:1 with Node

One of the most important parts of an alternative JS runtime, is its compatibility with Node APIs, these have been around a long time, used by many people and won’t be going away anytime soon. This makes it all the more critical to be compatible with them. It would be interesting to see how it handles much more complex applications, but from reading through documentation they support a huge number of the most common and uncommon APIs, which makes it dead simple to convert your application over.

Final thoughts

Before I can give any truly glowing review, I would need to use Bun in anger. Sure, it’s great to have a simple API working with Bun, but what happens with existing code? How much would I need to rewrite to get something pre-existing working? This is what I intend to find out. Hopefully, with work starting back in the next week, I can carve out some time to convert something to Bun. This would be a great example of swapping out Node for Bun and how it goes. If I end up getting some time to do this, I will write a write-up on how this went. For now, Bun is awesome, and I can’t wait to use it as an alternative to Node.

If you are interested in a simple login/register API with Hono + Bun, checkout this repo

If you enjoyed this article, you might enjoy my book “Achieving Your Potential: A Guide for Software Engineers”. It gives you all the information to build a successful career as a software engineer.