正在加载,请稍候…

Bun: The Fast All-in-One JavaScript Runtime and Toolkit

Explore Bun as a JavaScript runtime, package manager, bundler, and test runner. Compare performance with Node.js and learn when to use Bun in production.

Bun: The Fast All-in-One JavaScript Runtime

Bun is a fast all-in-one JavaScript runtime built with Zig, using JavaScriptCore (Safari engine). It is significantly faster than Node.js and includes a package manager, bundler, and test runner.

Speed: The Headline Feature

# Package installation comparison
npm install        # ~15 seconds
yarn install       # ~10 seconds
bun install        # ~1.5 seconds  # 10x faster!

# Script startup time
node -e "console.log('hi')"   # ~60ms
bun -e "console.log('hi')"    # ~6ms  # 10x faster!

HTTP Server

const server = Bun.serve({
  port: 3000,
  fetch(request) {
    const url = new URL(request.url);
    
    if (url.pathname === "/") {
      return new Response("Hello from Bun!");
    }
    
    if (url.pathname === "/json") {
      return Response.json({ 
        runtime: "Bun",
        version: Bun.version 
      });
    }
    
    return new Response("Not Found", { status: 404 });
  },
});

File I/O

// Reading files - faster than Node.js fs
const file = Bun.file("./data.json");
const content = await file.text();
const json = await file.json();

// Writing files
await Bun.write("./output.txt", "Hello, Bun!");
await Bun.write("./data.json", JSON.stringify({ key: "value" }));

Built-in Test Runner

import { test, expect, describe } from "bun:test";

describe("Calculator", () => {
  test("adds two numbers", () => {
    expect(2 + 3).toBe(5);
  });
  
  test("async test", async () => {
    const res = await fetch("https://example.com");
    expect(res.ok).toBe(true);
  });
});
bun test
bun test --watch
bun test --coverage

SQLite Integration

import { Database } from "bun:sqlite";

const db = new Database(":memory:");
db.run("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");

const insert = db.prepare("INSERT INTO users (name) VALUES (?)");
insert.run("Alice");
insert.run("Bob");

const users = db.query("SELECT * FROM users").all();
console.log(users); // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]

Bundler

# Bundle TypeScript for the browser
bun build ./src/index.ts --outdir ./dist --minify

# Or via API
await Bun.build({
  entrypoints: ["./src/index.ts"],
  outdir: "./dist",
  target: "browser",
  minify: true,
});

Performance Benchmarks

Operation Node.js Bun
HTTP req/s ~85k ~170k
npm install ~15s ~1.5s
TypeScript startup ~100ms ~8ms
SQLite queries/s ~500k ~1.2M

When to Use Bun

  • New projects: Excellent default choice
  • CLI tools: Fast startup time matters
  • SQLite apps: Best-in-class SQLite performance
  • TypeScript apps: Native TS support, no build step needed

Summary

Bun is production-ready and offers compelling performance advantages. Built-in SQLite, test runner, and bundler reduce tooling complexity. For new JavaScript/TypeScript projects, Bun is worth considering seriously.