Introduction
Bun is a modern JavaScript runtime that aims to be faster and more efficient than Node.js. Written in Zig, Bun combines a runtime, bundler, test runner, and package manager in one tool.
This guide covers Bun for JavaScript developers in 2026.
What is Bun?
Overview
Bun is designed as an all-in-one JavaScript toolkit:
- JavaScript runtime (like Node.js)
- Bundler (like webpack)
- Test runner (like Jest)
- Package manager (like npm)
Installation
# Install Bun
curl -fsSL https://bun.sh/install | bash
# Verify installation
bun --version
Bun vs Node.js
Performance Comparison
Startup time:
- Node.js: ~50ms
- Bun: ~5ms โก
HTTP throughput:
- Node.js: ~50k req/s
- Bun: ~150k req/s โก
Key Differences
| Feature | Node.js | Bun |
|---|---|---|
| Engine | V8 | JavaScriptCore |
| Language | JavaScript | Zig |
| Package Manager | npm | Built-in |
| Bundler | webpack | Built-in |
| Test Runner | Jest/Vitest | Built-in |
Using Bun
Running Scripts
// hello.js
console.log("Hello from Bun!");
// Run with Bun
bun run hello.js
HTTP Server
const server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Hello, World!");
},
});
console.log(`Server running at http://${server.hostname}:${server.port}`);
File Operations
const file = Bun.file("data.json");
const text = await file.text();
const json = await file.json();
// Write file
await Bun.write("output.txt", "Hello!");
Package Management
Install Dependencies
# Install all dependencies
bun install
# Add package
bun add express
# Add dev dependency
bun add -d vitest
Run Scripts
{
"scripts": {
"dev": "bun run src/index.js",
"test": "bun test"
}
}
bun run dev
bun test
Bun APIs
Fetch API
const response = await fetch("https://api.example.com/data");
const data = await response.json();
WebSocket
const ws = new WebSocket("wss://example.com");
ws.onmessage = (event) => {
console.log(event.data);
};
SQLite
const db = new Bun.Database("database.sqlite");
db.exec(`
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)
`);
db.exec("INSERT INTO users (name) VALUES ('John')");
Testing with Bun
Basic Test
import { test, expect } from "bun:test";
test("2 + 2 equals 4", () => {
expect(2 + 2).toBe(4);
});
test("async function", async () => {
const result = await Promise.resolve(42);
expect(result).toBe(42);
});
Run Tests
bun test
bun test --coverage
Conclusion
Bun offers impressive performance and an all-in-one toolkit. Try it for new projects to experience the speed improvements.
Comments