본문으로 건너뛰기 Complete Bun Complete Guide | Ultra-Fast JavaScript Runtime

Complete Bun Complete Guide | Ultra-Fast JavaScript Runtime

Complete Bun Complete Guide | Ultra-Fast JavaScript Runtime

이 글의 핵심

Complete guide to implementing fast JavaScript development with Bun. Node.js alternative, fast package installation, built-in bundler, test runner with ...

Key Takeaways

Complete guide to implementing fast JavaScript development with Bun. Covers Node.js alternative, fast package installation, built-in bundler, and test runner with practical examples.

Real-World Experience: Sharing experience of transitioning from Node.js to Bun, where package installation became 10x faster and test execution became 5x faster.

Introduction: “Node.js is Slow”

Real-World Problem Scenarios

Scenario 1: npm install is too slow
npm is slow. Bun is 10x faster. Scenario 2: Bundler configuration is complex
Webpack is complex. Bun provides built-in bundler. Scenario 3: Tests are slow
Jest is slow. Bun is 5x faster.

1. What is Bun?

Core Features

Bun is an ultra-fast JavaScript runtime. Key Advantages:

  • Fast Speed: 4x faster than Node.js
  • All-in-One: Runtime + Package Manager + Bundler + Test
  • Node.js Compatible: Mostly compatible
  • TypeScript: Native support
  • Web API: Built-in fetch, WebSocket Performance Comparison:
  • Node.js: 100ms
  • Deno: 80ms
  • Bun: 25ms

Real-World Adoption

Bun is rapidly gaining traction as the fastest JavaScript runtime:

Created by Jarred Sumner:

  • Built from scratch in Zig - systems programming language for maximum performance
  • Started 2021 - youngest of the major JavaScript runtimes
  • Funded by $7M Series A (2023) to build the team

Production Usage:

  • Vercel: Testing Bun for build pipelines (reported 3x faster installs)
  • Linear: Using Bun in development workflows
  • Payload CMS: Offers Bun as installation option
  • Cloudflare: Evaluating Bun for Workers compatibility

Market Growth:

  • 1.5+ million weekly npm downloads (April 2026) - explosive growth
  • 72,000+ GitHub stars - fastest-growing JS runtime project
  • 500,000+ installs in first 2 years
  • Used in 50,000+ repositories

Why Developers Love Bun:

  • 4x faster startup than Node.js (JavaScriptCore engine)
  • 10x faster package installs - global cache, parallel downloads
  • All-in-one: Runtime + bundler + test runner + package manager
  • Drop-in Node replacement: 90%+ Node.js API compatible
  • Native TypeScript: No compilation step needed

Performance Benchmarks:

  • bun install: 6s (npm: 60s, pnpm: 20s)
  • HTTP server: 4x faster than Express on Node
  • Test runner: 5x faster than Jest
  • Cold start: 25ms (Node: 100ms)

Ecosystem Compatibility:

  • ✅ Express, Fastify, Hono work out of the box
  • ✅ React, Vue, Svelte - all compatible
  • ✅ Most npm packages work (90%+ compatibility)
  • ⚠️ Native addons require recompilation
  • ⚠️ Some Node-specific APIs still in progress

When to Choose Bun:

  • ✅ New projects prioritizing speed
  • ✅ Monorepos with slow installs
  • ✅ Scripts and CLIs
  • ✅ Development environments
  • ⏳ Production (test thoroughly first - API surface still evolving)
  • ❌ Apps with many native addons
  • ❌ Enterprise requiring LTS stability

Community Impact:

  • Pushed Node.js to improve performance (Node 20+ faster)
  • Inspired pnpm and yarn to optimize further
  • Proved Zig viable for systems-level JS tooling

Bun is the fastest option today, but verify your specific use case - Node.js’s maturity still matters for many production workloads.


2. Installation and Basic Usage

Installation

# macOS/Linux
curl -fsSL https://bun.sh/install | bash
# Windows
powershell -c "irm bun.sh/install.ps1 | iex"

Basic Commands

# Run file
bun run index.ts
# REPL
bun
# Check version
bun --version

3. Package Management

Package Installation

# Install dependencies
bun install
# Add package
bun add express
bun add -d typescript
# Remove package
bun remove express
# Global install
bun add -g typescript

Speed Comparison:

  • npm: 60 seconds
  • pnpm: 20 seconds
  • bun: 6 seconds

4. Web Server

HTTP Server

// 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 === '/api/users') {
      return Response.json([
        { id: 1, name: 'John' },
        { id: 2, name: 'Jane' },
      ]);
    }
    return new Response('Not Found', { status: 404 });
  },
});
console.log(`Server running on http://localhost:${server.port}`);

Using Express

import express from 'express';
const app = express();
app.get('/', (req, res) => {
  res.send('Hello Bun with Express!');
});
app.listen(3000, () => {
  console.log('Server running on :3000');
});

5. Bundler

Basic Bundling

// build.ts
await Bun.build({
  entrypoints: ['./src/index.ts'],
  outdir: './dist',
  target: 'browser',
  minify: true,
  sourcemap: 'external',
});

React Bundling

await Bun.build({
  entrypoints: ['./src/index.tsx'],
  outdir: './dist',
  target: 'browser',
  minify: true,
  splitting: true,
  loader: {
    '.png': 'file',
    '.svg': 'file',
  },
});

6. Testing

Basic Tests

// math.test.ts
import { expect, test, describe } from 'bun:test';
describe('Math', () => {
  test('add', () => {
    expect(1 + 2).toBe(3);
  });
  test('multiply', () => {
    expect(2 * 3).toBe(6);
  });
});

Async Tests

import { expect, test } from 'bun:test';
test('fetch users', async () => {
  const response = await fetch('https://api.example.com/users');
  const users = await response.json();
  expect(users).toBeArray();
  expect(users.length).toBeGreaterThan(0);
});

Run

bun test

7. File System

Reading Files

// Text file
const text = await Bun.file('data.txt').text();
// JSON file
const json = await Bun.file('data.json').json();
// Binary file
const buffer = await Bun.file('image.png').arrayBuffer();

Writing Files

await Bun.write('output.txt', 'Hello Bun!');
await Bun.write('data.json', JSON.stringify({ name: 'John' }));

8. Environment Variables

// .env
DATABASE_URL=postgresql://localhost:5432/mydb
API_KEY=secret123
// Usage
console.log(process.env.DATABASE_URL);
console.log(Bun.env.API_KEY);

9. Hot Reload

bun --watch server.ts

10. Real-World Example

REST API

// api/server.ts
const server = Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === '/api/users' && req.method === 'GET') {
      const users = await db.select().from(usersTable);
      return Response.json(users);
    }
    if (url.pathname === '/api/users' && req.method === 'POST') {
      const body = await req.json();
      const user = await db.insert(usersTable).values(body).returning();
      return Response.json(user[0], { status: 201 });
    }
    return new Response('Not Found', { status: 404 });
  },
});

Summary and Checklist

Key Summary

  • Bun: Ultra-fast JavaScript runtime
  • All-in-One: Runtime + Package Manager + Bundler + Test
  • Fast Speed: 4x faster than Node.js
  • TypeScript: Native support
  • Node.js Compatible: Mostly compatible
  • Web API: Built-in fetch, WebSocket

Implementation Checklist

  • Install Bun
  • Initialize project
  • Implement web server
  • Configure bundling
  • Write tests
  • Use file system
  • Deploy

  • Complete pnpm Guide
  • Complete Vite 5 Guide
  • Complete Vitest Guide

Keywords Covered

Bun, JavaScript, Runtime, Package Manager, Bundler, Performance, Node.js

Frequently Asked Questions (FAQ)

Q. Can it completely replace Node.js?

A. Possible in most cases, but some native modules may not be compatible.

Q. Is it safe to use in production?

A. Still version 1.0, but used in many projects. If stability is important, Node.js is recommended.

Q. Can I use npm packages?

A. Yes, most npm packages are compatible.

Q. Does it support Windows?

A. Yes, Windows is supported.


Frequently Asked Questions (FAQ)

Q. When would I use this in practice?

A. Complete guide to implementing fast JavaScript development with Bun.

Q. What should I read before this?

A. Follow the previous article or related articles links at the bottom of each post to learn in sequence.

Q. Where can I study this more deeply?

A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.


Other articles related to this topic.


Keywords Covered in This Article (Related Search Terms)

This article covers Bun, JavaScript, Runtime, Package Manager, Bundler, Performance, Node.js.