Bun v1.0 — fast all-in-one JS runtime

リン (linh)
Goalist Blog
Published in
4 min readOct 12, 2023

I. What is Bun

Bun is an all-in-one toolkit for JavaScript and TypeScript apps. It ships as a single executable called bun.
src:
https://bun.sh/docs

So, basically, it’s a JS runtime, but at the same time, it can bundle files, install packages, run tests.

People currently focus a lot on the “fast” aspect of Bun, but i just want to take a look on it’s features and the experiences of setting up as well as using it for the first time.

II. Features

There are a lot going on with Bun but i find below features draw my attention the most. They’re all built-in instead of having to install like on Nodejs.

  • Install Bun
curl -fsSL https://bun.sh/install | bash
  • Create a new repo for your Bun project
  • Run below command and let bun install neccessary stuffs
bun init

① Typescript

Regarding typescript, there’s actually nothing we have to do, after the setup, bun-types was installed and you can start using typescripts as normal.

// you can see that bun-types is added to tsconfig as below
{
"compilerOptions": {
"types": ["bun-types"]
}
}

② Environment variables

We no longer need to install dotenv or dotevn-expand, you can just call process.env.API_TOKEN or Bun.env.API_TOKEN, for example.

③ Module import

import { hello } from "./hello" is ok to use with Bun.
CommonJS modules (require()/module.exports) is acceptable, but it’s not recommended in order to keep the consistency if you start a new project.

④ Watch

Forget nodemon, with Bun, you just need to add script to watch the changes like below

"scripts": {
"start": "bun run src/index.ts",
"dev": "bun --watch src/index.ts"
},

Now everytime you make chages, your app restart. You can also watch the test, so that tests will run when you make changes on test case.
Beside --watch, you can also use --hot, which does not hard-restart the entire process. Instead, it detects code changes and updates its internal module cache with the new code.

⑤ Websocket

Yes, Bun support Websocket by default. Below is a basic websocket server in Bun.

Bun.serve({
fetch(req, server) {
// upgrade the request to a WebSocket
if (server.upgrade(req)) {
return; // do not return a Response
}
return new Response("Upgrade failed :(", { status: 500 });
},
websocket: {
message(ws, message) {}, // a message is received
open(ws) {}, // a socket is opened
close(ws, code, message) {}, // a socket is closed
drain(ws) {}, // the socket is ready to receive more data
},
});

Beside sending message, you can also:

  • handle contextual data, which is data that contains more than just a string, you can get userid, for example.
  • implements a native publish-subscribe API, which is perfect to build your chat app, where many sockets can subscribe to a topic and publish messages to all other subscribers of that topic.

⑥ SQLite

Bun natively implements a high-performance SQLite3 driver with below supported features:

  • Transactions
  • Parameters (named & positional)
  • Prepared statements
  • Datatype conversions (BLOB becomes Uint8Array)
  • The fastest performance of any SQLite driver for JavaScript
import { Database } from "bun:sqlite";
const db = new Database(":memory:");
const query = db.query("select 'Hello world' as message;");
query.get(); // => { message: "Hello world" }

⑦ Test

Bun ships with a fast, built-in, Jest-compatible test runner with below supported features:

  • TypeScript and JSX
  • Lifecycle hooks
  • Snapshot testing
  • UI & DOM testing
  • Watch mode with --watch
  • Script pre-loading with --preload
import { expect, test } from "bun:test";
test("2 + 2", () => {
expect(2 + 2).toBe(4);
});

Please be noted that mock() function is not implemented yet, but you can use alternative way to achieve the same thing.

import { test, expect, jest } from "bun:test";
const random = jest.fn(() => Math.random());

III. Conclusion

Above are just those that draw my most attention but there are more and you can check out the details on official website. There are also guides in case you want to try Bun with frameworks or the things you care about.
Bun is still young and they have plans for it, but the fact that it removes lots of installations everytime I start a backend project in javascript, is worth mentioning. Hopefully, i’ll build something with Bun and share you in the near future.

--

--

リン (linh)
Goalist Blog

A career-changed-non-tech-background point of view.