Introduction

Bun 1.0 is a powerful toolkit for running, building, testing, and debugging JavaScript and TypeScript projects. From a single file to a full-stack application, Bun has got you covered. With its speed, efficiency, and native support for various web standard APIs, Bun is a game-changer for developers

What is Bun?

Bun is a JavaScript runtime, a bundler, and a whole lot more. It is designed to make the experience of building software faster, less frustrating, and more fun. It is also a drop-in replacement for Node.js, providing a minimal set of highly-optimized APIs for common tasks such as starting an HTTP server and writing files.

Features of Bun 1.0

Built-in Web APIs

Bun has built-in support for web standard APIs available in browsers. These include fetch, Request, Response, WebSocket, and ReadableStream. This eliminates the need to install packages like node-fetch and ws, as Bun's built-in Web APIs are implemented in native code, making them faster and more reliable.

const response = await fetch("https://example.com/");
const text = await response.text();

File Handling with Bun.file()

Bun offers a simple API for handling files. The Bun.file() function returns a BunFile object, which extends the Web standard File. The file contents can be lazily loaded in various formats.

const file = Bun.file("package.json");
const contents = await file.text();

Writing to Disk with Bun.write()

Bun.write() is a single, flexible API for writing almost anything to disk — string, binary data, Blobs, even a Response object.

await Bun.write("index.html", "<html/>");
await Bun.write("index.html", Buffer.from("<html/>"));
await Bun.write("index.html", Bun.file("home.html"));
await Bun.write("index.html", await fetch("https://example.com/"));

Built-in SQLite Support

Bun has built-in support for SQLite, providing an API that's inspired by better-sqlite3 but is written in native code for improved speed. Bun can query SQLite up to 4x faster than better-sqlite3 on Node.js

import { Database } from "bun:sqlite";
const db = new Database(":memory:");
const query = db.query("select 'Bun' as runtime;");
query.get(); // => { runtime: "Bun" }

Password Hashing with Bun.password

Bun also supports APIs for complex tasks like password hashing using bcrypt or argon2, without requiring any external dependencies

const password = "super-secure-pa$$word";
const hash = await Bun.password.hash(password);
const isMatch = await Bun.password.verify(password, hash); // => true

Bun as a Bundler

Bun is a JavaScript and TypeScript bundler and minifier that can be used to bundle code for the browser, Node.js, and other platforms. It's heavily inspired by esbuild and provides a compatible plugin API.

import mdx from "@mdx-js/esbuild";
Bun.build({
  entrypoints: ["index.tsx"],
  outdir: "build",
  plugins: [mdx()],
});

Bun's plugin API is universal, meaning it works for both the bundler and the runtime. Using esbuild's own benchmarks, Bun is 1.75x faster than esbuild, 150x faster than Parcel 2, 180x times faster than Rollup + Terser, and 220x times faster than Webpack.

The Future of Bun

Bun is continuously evolving to meet the needs of developers. Notably, Bun has introduced an experimental, native build for Windows. While the macOS and Linux builds of Bun are production-ready, the Windows build is highly experimental, with only the JavaScript runtime supported currently. The package manager, test runner, and bundler have been disabled until they are more stable.