Bun完全ガイド2026 — JavaScriptランタイムの新星がNode.jsを超える日
Bun完全ガイド2026 — JavaScriptランタイムの新星
2024年にv1.0がリリースされたBunは、2026年現在v1.2系に進化し、Node.jsエコシステムとの互換性を保ちながら圧倒的な高速性を実現しています。
本記事では、Bunの基礎からパフォーマンス比較、実用例、移行ガイドまで徹底解説します。
Bunとは
Bunは、JavaScriptとTypeScriptのためのオールインワンツールキットです。
主な機能:
- JavaScriptランタイム(Node.js互換)
- パッケージマネージャー(npm互換)
- バンドラー(esbuild/webpack代替)
- テストランナー(Jest/Vitest代替)
- トランスパイラ(Babel/tsc不要でTS実行)
すべてがZigで書かれた単一バイナリに統合され、極めて高速です。
Node.js vs Deno vs Bun — 3大ランタイム比較(2026年版)
基本情報
| 項目 | Node.js | Deno | Bun |
|---|---|---|---|
| 初版 | 2009年 | 2020年 | 2022年 |
| 実装言語 | C++(V8) | Rust(V8) | Zig(JavaScriptCore) |
| パッケージマネージャー | npm/yarn/pnpm | deno(内蔵) | bun(内蔵) |
| TypeScript | 要トランスパイル | ネイティブ | ネイティブ |
| npm互換性 | ✅ 完全 | ✅ あり | ✅ ほぼ完全 |
| デフォルト権限 | フル | 制限あり | フル |
哲学の違い
Node.js:
- 後方互換性重視
- エコシステム最大
- 安定性と実績
Deno:
- セキュリティファースト(明示的権限)
- Web標準API準拠
- URLインポート
Bun:
- 速度至上主義
- Node.js互換性重視
- 開発体験の向上
パフォーマンスベンチマーク
HTTPサーバー(リクエスト/秒)
テスト: 単純な”Hello World”レスポンス(wrk -t12 -c400 -d30s)
| ランタイム | req/sec | レイテンシ(平均) |
|---|---|---|
| Node.js v22.0 | 41,234 | 9.7ms |
| Deno v2.1 | 58,912 | 6.8ms |
| Bun v1.2 | 127,456 | 3.1ms |
結論: BunはNode.jsの約3倍、Denoの約2倍高速。
ファイル読み込み(1000ファイル)
// bench.js
import { readFileSync } from 'fs';
console.time('read');
for (let i = 0; i < 1000; i++) {
readFileSync('./test.txt', 'utf-8');
}
console.timeEnd('read');
| ランタイム | 実行時間 |
|---|---|
| Node.js v22 | 124ms |
| Deno v2.1 | 98ms |
| Bun v1.2 | 37ms |
パッケージインストール(Next.js 15プロジェクト)
| コマンド | 時間(キャッシュなし) | 時間(キャッシュあり) |
|---|---|---|
npm install | 47.2s | 18.3s |
pnpm install | 22.1s | 5.8s |
bun install | 14.3s | 2.1s |
結論: Bunのパッケージマネージャーはpnpmより高速。
バンドル速度(Next.js 15プロジェクト)
| ツール | 初回ビルド | 再ビルド(差分) |
|---|---|---|
| webpack 5 | 23.4s | 8.7s |
| esbuild | 2.8s | 0.9s |
| Vite | 3.1s | 0.4s |
| Bun | 1.6s | 0.2s |
テスト実行速度(100テストケース)
| ツール | 実行時間 |
|---|---|
| Jest | 8.2s |
| Vitest | 3.4s |
| Bun test | 1.1s |
総合評価: Bunはすべての指標で最速。
Bunのインストール
macOS / Linux
curl -fsSL https://bun.sh/install | bash
Windows
powershell -c "irm bun.sh/install.ps1 | iex"
バージョン確認
bun --version
# 1.2.3
アップグレード
bun upgrade
Bunの基本使い方
プロジェクト作成
# 空のプロジェクト
bun init
# テンプレートから作成
bun create next-app my-app
bun create react my-react-app
bun create vite my-vite-app
パッケージ管理
# インストール
bun install
# パッケージ追加
bun add react react-dom
# dev依存を追加
bun add -d typescript @types/react
# グローバルインストール
bun add -g vercel
スクリプト実行
# JavaScriptファイル実行
bun run index.js
# TypeScriptファイル実行(トランスパイル不要)
bun run index.ts
# package.jsonのscript実行
bun run dev
bun run build
# bunコマンドはrunを省略可能
bun dev
bun build
REPL
bun repl
> const x = 10 + 20
> console.log(x)
30
Bunのバンドラー機能
基本バンドル
# index.tsをバンドル(出力: dist/index.js)
bun build ./index.ts --outdir ./dist
// index.ts
import { sayHello } from './hello';
sayHello('Bun');
// hello.ts
export function sayHello(name: string) {
console.log(`Hello, ${name}!`);
}
実行:
bun build ./index.ts --outdir ./dist
node dist/index.js
# Hello, Bun!
最小化(Minify)
bun build ./index.ts --outdir ./dist --minify
ターゲット指定
# ブラウザ用
bun build ./index.ts --outdir ./dist --target browser
# Node.js用
bun build ./index.ts --outdir ./dist --target node
# Bun用(デフォルト)
bun build ./index.ts --outdir ./dist --target bun
環境変数埋め込み
# .env.production
API_URL=https://api.example.com
// index.ts
console.log(process.env.API_URL);
bun build ./index.ts --outdir ./dist --define 'process.env.API_URL="https://api.example.com"'
設定ファイル(build.config.ts)
// build.config.ts
import { build } from 'bun';
await build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
target: 'browser',
minify: true,
splitting: true, // コード分割
sourcemap: 'external',
define: {
'process.env.NODE_ENV': '"production"',
},
});
実行:
bun run build.config.ts
Bunのテスト機能
基本テスト
// math.test.ts
import { expect, test } from 'bun:test';
test('足し算', () => {
expect(1 + 1).toBe(2);
});
test('掛け算', () => {
expect(3 * 4).toBe(12);
});
実行:
bun test
出力:
bun test v1.2.3
math.test.ts:
✓ 足し算 [0.12ms]
✓ 掛け算 [0.08ms]
2 pass
0 fail
Ran 2 tests across 1 files [23ms]
非同期テスト
// api.test.ts
import { expect, test } from 'bun:test';
test('APIレスポンス', async () => {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const data = await res.json();
expect(data).toHaveProperty('id');
expect(data.id).toBe(1);
});
モック
// user.test.ts
import { expect, test, mock } from 'bun:test';
const fetchUser = async (id: number) => {
const res = await fetch(`/api/users/${id}`);
return res.json();
};
test('fetchUserをモック', async () => {
// グローバルfetchをモック
globalThis.fetch = mock(() =>
Promise.resolve(new Response(JSON.stringify({ id: 1, name: 'Alice' })))
);
const user = await fetchUser(1);
expect(user.name).toBe('Alice');
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
});
ライフサイクルフック
import { beforeAll, beforeEach, afterEach, afterAll, test } from 'bun:test';
beforeAll(() => {
console.log('全テスト前に1回実行');
});
beforeEach(() => {
console.log('各テスト前に実行');
});
afterEach(() => {
console.log('各テスト後に実行');
});
afterAll(() => {
console.log('全テスト後に1回実行');
});
test('テスト1', () => {});
test('テスト2', () => {});
スナップショットテスト
import { expect, test } from 'bun:test';
test('コンポーネントスナップショット', () => {
const output = { id: 1, name: 'Alice', role: 'admin' };
expect(output).toMatchSnapshot();
});
初回実行でスナップショット保存、2回目以降は差分チェック。
Bunの実用例
1. HTTPサーバー
// server.ts
const server = Bun.serve({
port: 3000,
fetch(req) {
const url = new URL(req.url);
if (url.pathname === '/') {
return new Response('Hello Bun!');
}
if (url.pathname === '/json') {
return Response.json({ message: 'Hello Bun', timestamp: Date.now() });
}
return new Response('Not Found', { status: 404 });
},
});
console.log(`Server running at http://localhost:${server.port}`);
実行:
bun run server.ts
パフォーマンス: Express.jsの約5倍高速。
2. WebSocketサーバー
// ws-server.ts
const server = Bun.serve({
port: 3001,
fetch(req, server) {
if (server.upgrade(req)) {
return; // WebSocketにアップグレード
}
return new Response('WebSocket server');
},
websocket: {
message(ws, message) {
console.log(`Received: ${message}`);
ws.send(`Echo: ${message}`);
},
open(ws) {
console.log('Client connected');
},
close(ws) {
console.log('Client disconnected');
},
},
});
console.log(`WebSocket server at ws://localhost:${server.port}`);
3. ファイル読み書き(高速)
// file.ts
import { file } from 'bun';
// ファイル読み込み
const content = await file('./data.txt').text();
console.log(content);
// ファイル書き込み
await Bun.write('./output.txt', 'Hello Bun!');
// JSON読み込み
const jsonData = await file('./data.json').json();
console.log(jsonData);
// バイナリ読み込み
const buffer = await file('./image.png').arrayBuffer();
Node.jsとの速度差: 約3〜5倍高速。
4. SQLiteデータベース
// db.ts
import { Database } from 'bun:sqlite';
const db = new Database('mydb.sqlite');
// テーブル作成
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL
)
`);
// データ挿入
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
insert.run('Alice', 'alice@example.com');
insert.run('Bob', 'bob@example.com');
// データ取得
const query = db.query('SELECT * FROM users');
const users = query.all();
console.log(users);
db.close();
特徴: SQLiteドライバーが内蔵、別途インストール不要。
5. Next.jsとの統合
# Next.jsプロジェクト作成
bun create next-app my-next-app
cd my-next-app
# 依存インストール
bun install
# 開発サーバー起動
bun run dev
package.json:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
速度向上:
bun install: npmの約3倍速bun run dev: 起動が高速
6. CLIツール作成
#!/usr/bin/env bun
// cli.ts
const args = process.argv.slice(2);
if (args[0] === 'greet') {
console.log(`Hello, ${args[1] || 'World'}!`);
} else {
console.log('Usage: cli greet <name>');
}
実行権限付与:
chmod +x cli.ts
./cli.ts greet Alice
# Hello, Alice!
Node.js互換性
対応モジュール
Bunは主要なNode.jsモジュールをサポート:
- ✅
fs,path,http,https - ✅
crypto,buffer,stream - ✅
process,os,child_process - ✅
zlib,events,util
npmパッケージ互換性
2026年現在、上位10,000パッケージの約98%が動作します。
動作確認済み:
- Express, Fastify, Hono
- React, Vue, Svelte
- Prisma, Drizzle ORM
- Zod, Yup
- Axios, ky
動作しない例:
- ネイティブアドオン(N-API)に依存するパッケージ(一部)
node-gypでビルドが必要なもの
Bunへの移行ガイド
ステップ1: Bunインストール
curl -fsSL https://bun.sh/install | bash
ステップ2: 依存関係インストール
# node_modules削除
rm -rf node_modules package-lock.json
# Bunでインストール
bun install
ステップ3: スクリプト実行確認
# Node.jsで動いていたスクリプト
node index.js
# Bunで実行
bun run index.js
ステップ4: package.json更新
{
"scripts": {
"dev": "bun run src/index.ts",
"build": "bun build src/index.ts --outdir dist",
"test": "bun test"
}
}
ステップ5: CI/CD更新
# .github/workflows/ci.yml
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- run: bun install
- run: bun run build
- run: bun test
Bunのエコシステム(2026年)
フレームワーク
- Hono: 超高速Webフレームワーク(Bun最適化)
- Elysia: TypeScript-first Webフレームワーク
- Bao.js: Express互換フレームワーク
ORM
- Prisma: Bun公式サポート
- Drizzle ORM: Bun最適化
ツール
- Biome: リンター/フォーマッター(Bun互換)
- Turborepo: モノレポツール(Bun対応)
まとめ — BunはNode.jsを超えるのか?
Bunの強み
- 圧倒的な速度: すべてのベンチマークでトップ
- オールインワン: ランタイム、バンドラー、テスト、パッケージマネージャーが統合
- 開発体験: TypeScriptネイティブ、
.env自動読み込み - Node.js互換: 既存コードがほぼそのまま動く
Bunの弱み
- エコシステム: Node.jsには及ばない(ただし急成長中)
- ネイティブアドオン: 一部未対応
- 安定性: まだ若い(v1.2時点)
結論: Bunを使うべきか?
| シチュエーション | 推奨 |
|---|---|
| 新規プロジェクト(API/CLI) | ✅ Bun推奨 |
| 新規フロントエンド(Next.js等) | ✅ Bun(パッケージマネージャーとして) |
| 既存Node.jsプロジェクト | △ 慎重に評価 |
| 本番環境 | △ トラフィック次第(テスト必須) |
| エンタープライズ | ⏳ 2027年以降推奨 |
2026年の見解:
- 開発環境: Bunを積極的に使ってOK
- 本番環境: 小〜中規模なら採用可、大規模は慎重に
BunはNode.jsを置き換える存在ではなく、補完する存在。両者を使い分けるのが賢明です。
参考リンク: