Bun — A fast all-in-one JavaScript runtime
Bun is joining Anthropic & Anthropic is betting on Bun →
Bun is a fast, incrementally adoptable all-in-one JavaScript, TypeScript & JSX toolkit. Use individual tools like bun test or bun install in Node.js projects, or adopt the complete stack with a fast JavaScript runtime, bundler, test runner, and package manager built in. Bun aims for 100% Node.js compatibility.
Bundling 10,000 React components
Build time in milliseconds (Linux x64, Hetzner)
Express.js 'hello world'
HTTP requests per second (Linux x64)
bun: 59,026 requests per second
deno: 25,335 requests per second
node: 19,039 requests per second
WebSocket chat server
Messages sent per second (Linux x64, 32 clients)
bun: 2,536,227 messages sent per second
deno: 1,320,525 messages sent per second
node: 435,099 messages sent per second
Load a huge table
Queries per second. 100 rows x 100 parallel queries
bun: 28,571 queries per second
node: 14,522 queries per second
deno: 11,169 queries per second
Use them together as an all-in-one toolkit, or adopt them incrementally. bun test works in Node.js projects. bun install can be used as the fastest npm client. Each tool stands on its own.
Who uses Bun?
Bun's all-in-one toolkit makes Railway's serverless functions fast and easy to use.
What's different about Bun?
Bun provides extensive builtin APIs and tooling
Everything you need to build & ship
Production-ready APIs and tools, built into Bun
Bun is a new JavaScript runtime built from scratch to serve the modern JavaScript ecosystem. It has three major design goals:
- Bun starts fast and runs fast. It extends JavaScriptCore, the performance-minded JS engine built for Safari. Fast start times mean fast apps and fast APIs.
- Bun provides a minimal set of highly-optimized APIs for performing common tasks, like starting an HTTP server and writing files.
- Bun is a complete toolkit for building JavaScript apps, including a package manager, test runner, and bundler.
Bun is designed as a drop-in replacement for Node.js. It natively implements thousands of Node.js and Web APIs, including fs, path, Buffer and more.
The goal of Bun is to run most of the world's server-side JavaScript and provide tools to improve performance, reduce complexity, and multiply developer productivity.
Bun works with Next.js
Full speed full-stack
Fast frontend apps with Bun's built-in high performance development server and production bundler. You've never seen hot-reloading this fast!
Develop and ship frontend apps
Bun's built-in bundler and dev server make frontend development fast and simple. Develop with instant hot reload, then ship optimized production builds—all with zero configuration.
Start a dev server
Run bun ./index.html to start a dev server. TypeScript, JSX, React, and CSS imports work out of the box.
Hot Module Replacement
Built-in HMR preserves application state during development. Changes appear instantly—no manual refresh needed.
Build for production
Build optimized bundles with bun build ./index.html --production. Tree-shaking, minification, and code splitting work out of the box.
Installing dependencies from cache for a Remix app.
View benchmark
Replace yarn with bun install to get 30x faster package installs.
Replace jest with bun test to run your tests 10-30x faster.
Start an HTTP server
Start a WebSocket server
Read and write files
Hash a password
Frontend dev server
Write a test
Query PostgreSQL
Use Redis
Import YAML
Set cookies
Run a shell script
Call a C function
index.tsx
import { sql, serve } from "bun";
const server = serve({
port: 3000,
routes: {
"/": new Response("Welcome to Bun!"),
"/api/users": async (req) => {
const users = await sql`SELECT * FROM users LIMIT 10`;
return Response.json({ users });
},
},
});
console.log(`Listening on localhost:${server.port}`);index.tsx
const server = Bun.serve<{ authToken: string; }>({
fetch(req, server) {
// use a library to parse cookies
const cookies = parseCookies(req.headers.get("Cookie"));
server.upgrade(req, {
data: { authToken: cookies['X-Token'] },
});
},
websocket: {
// handler called when a message is received
async message(ws, message) {
console.log(`Received: ${message}`);
const user = getUserFromToken(ws.data.authToken);
await db.Message.insert({
message: String(message),
userId: user.id,
});
},
},
});
console.log(`Listening on localhost:${server.port}`);index.tsx
const file = Bun.file(import.meta.dir + '/package.json'); // BunFile
const pkg = await file.json(); // BunFile extends Blob
pkg.name = 'my-package';
pkg.version = '1.0.0';
await Bun.write(file, JSON.stringify(pkg, null, 2));
index.tsx
const password = "super-secure-pa$$word";
const hash = await Bun.password.hash(password);
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh...
const isMatch = await Bun.password.verify(password, hash);
// => trueserver.ts
// Run 'bun init --react' to get started
import { serve } from "bun";
import reactApp from "./index.html";
serve({
port: 3000,
routes: {
"/": reactApp,
"/api/hello": () => Response.json({ message: "Hello!" }),
},
development: {
console: true, // Stream browser logs to terminal
hmr: true, // Enable hot module reloading
},
});index.test.tsx
import { test, expect } from "bun:test";
// Run tests concurrently for better performance
test.concurrent("fetch user 1", async () => {
const res = await fetch("https://api.example.com/users/1");
expect(res.status).toBe(200);
});
test.concurrent("fetch user 2", async () => {
const res = await fetch("https://api.example.com/users/2");
expect(res.status).toBe(200);
});
test("addition", () => {
expect(2 + 2).toBe(4);
});
index.tsx
import { sql } from "bun";
// Query with automatic SQL injection prevention
const users = await sql`
SELECT * FROM users
WHERE active = ${true}
LIMIT 10
`;
// Insert with object notation
const [user] = await sql`
INSERT INTO users ${sql({
name: "Alice",
email: "alice@example.com"
})}
RETURNING *
`;config.yaml
// Import YAML files directly
import config from "./config.yaml";
console.log(config.database.host);
// => "localhost"
// Or parse YAML at runtime
const data = Bun.YAML.parse(`
name: my-app
version: 1.0.0
database:
host: localhost
port: 5432
`);index.tsx
import { serve } from "bun";
serve({
port: 3000,
routes: {
"/": (request) => {
// Read cookies with built-in parsing
const sessionId = request.cookies.get("session_id");
// Set cookies
request.cookies.set("session_id", "abc123", {
path: "/",
httpOnly: true,
secure: true,
});
return Response.json({ success: true });
},
},
});index.tsx
import { redis } from "bun";
// Set a key
await redis.set("greeting", "Hello from Bun!");
console.log(db.query("SELECT 1 as x").get());
// { x: 1 }
index.tsx
import { $ } from 'bun';
// Run a shell command (also works on Windows!)
await $`echo "Hello, world!"`;
const response = await fetch("https://example.com");
// Pipe the response body to gzip
const data = await $`gzip < ${response}`.arrayBuffer();index.tsx
import { dlopen, FFIType, suffix } from "bun:ffi";
// `suffix` is either "dylib", "so", or "dll" depending on the platform
const path = `libsqlite3.${suffix}`;
const {
symbols: {
sqlite3_libversion, // the function to call
},
} = dlopen(path, {
sqlite3_libversion: {
args: [], // no arguments
returns: FFIType.cstring, // returns a string
},
});
console.log(`SQLite 3 version: ${sqlite3_libversion()}`);