Bun 1.0 is out
- 4 minutes read - 788 wordsBun 1.0 is out of the oven! And it’s a big deal, because there wasn’t much happening in terms of performance or innovation in the recent years with NodeJS. Sure we have Deno, but who is really using Deno? Also Bun tries to combine various tools into one tool, helping out the developers a lot.
Pros
Bun is developed in the Zig programming language. Bun is very focused on performance, which in most cases the Bun Runtime (let’s call it buntime 😂) is indeed running faster than NodeJS. Mainly because of Zip and using JavaScriptCore Engine by Apple instead of the V8 JavaScript Engine by Google. Both engines are open-source.
Moreover, Buntime can also run TypeScript directly without the need of tsc
, so Bun transpiles the code for you. Bun tries to be API and tooling compatible with Node and NPM (more on that later).
I do like the fast built-in APIs for file IO which allows you to read or write files from disk. In fact much faster than the NodeJS alternative packages. You also have a built-in HTTP server (Bun.serve
) in Bun. Next, Bun is not only a runtime but also a package manager command to replace the npm
command. You can now just run: bun install
, which is indeed fast to be honest.
In Bun you also have top-level await and environment variables are supported out-of-the-box. And Bun even comes with a built-in test runner that is Jest-compatible, just execute: bun test
.
Bun also supports hot reload without the need of Nodemon or other tools, use: bun --hot
or: bun --watch
. Try for instance: bun --watch test
.
Last but not least, Bun allows you to finally combine CommonJS and ES modules imports within the same project and file (no need for "type": "module"
in your package.json
file and converting all your imports and exports):
import lodash from "lodash";
const _ = require("underscore");
Free pro tip:
If you are using the Express framework experiment with alternative implementations like Hono or Elysia, which can handle much more requests per second. For instance using Express with node on JSON Post data gives you an average of
14.361
req/s, while Hono or Elysia using JSON Post data on Bun can go up to respectively223.139
req/s and214.370
req/s. Assuming you really need those kind of performances 😉.
- Source of benchmarks
Cons
This all sounds fantastic but are there no down-sides? Yes there are…
When I tried to migrate some of my projects to Bun, I found out that Bun 1.0 wasn’t as “production ready” as Bun might tell you.
First of all, the official oven/bun
Docker image doesn’t come with an Alpine Linux version (yet). Meaning all Bun Docker images are almost 100MB in compressed size.
Changing the WORKDIR
in my Dockerfile resulted into:
Cannot find module ".//app/" from "bun:main"` error.
Then I miss the bun audit
command to my surprise. The bun outdated
command is missing as well… 😢
The idea is that Bun is a drop-in replacement for both Node and npm
, but npm advises people to use the --omit
flag over the previous --production
flag (eg. npm install --omit=dev
). Too bad Bun does not have this --omit
flag, so be sure to use: bun install --production
(or: -p
for short) instead. So again bun it not fully compatible with npm.
And when trying to build my JS code using: bun build
, I got the following error (the cluster package isn’t implemented in Bun yet):
Could not resolve Node.js builtin: "cluster". To use Node.js builtins, set target to 'node' or 'bun'
Keep in mind Bun will create a binary lock file (bun.lockb
), if you want to read the content, you need to execute: bun bun.lockb
. Therefore, a git diff on this file is also not straightforward anymore.
Ps. Do not believe the bun install
speeds in the official Bun blog post, since they used packages from cache which is not a fair comparison in my opinion. Yes, the bun install
command is faster then npm install
command but not always that fast.
Ps. Ps. Running bun run lint
(which executes eslint
in my case), will just use NodeJS runtime executable for some reason… If you really wish to run Bun you need to run: bun run --bun lint
!?
Conclusion
Overall, I recommend trying out Bun for yourself. I think Bun is a great step in the right direction. I think the Bun project with the 1.0 release also woke-up the NodeJS project.
Check for yourself whether Bun is working for you and worth the effort to migrate. I personally can’t wait for the above issues to be resolved, since this is a showstopper for me now.