DEV Community

Alex Spinov
Alex Spinov

Posted on

Bun Has a Free API: The All-in-One JavaScript Runtime That Replaces Node.js

What is Bun?

Bun is a JavaScript runtime, bundler, test runner, and package manager — all in one binary. Written in Zig and powered by JavaScriptCore (Safari's engine), it's 3-10x faster than Node.js for most operations.

Why Bun Over Node.js?

  • 3-10x faster — starts in 6ms vs Node's 50ms+
  • All-in-one — replaces node, npm, npx, jest, webpack, esbuild
  • Node.js compatible — runs most npm packages without changes
  • Built-in SQLite — bun:sqlite module for embedded databases
  • Built-in test runner — jest-compatible, no extra dependencies
  • TypeScript native — runs .ts files directly, no compilation step

Quick Start

# Install Bun
curl -fsSL https://bun.sh/install | bash

# Create project
bun init

# Run TypeScript directly
bun run index.ts

# Install packages (10-100x faster than npm)
bun install express @types/express
Enter fullscreen mode Exit fullscreen mode

HTTP Server (Built-in)

// server.ts — no Express needed for simple APIs
const server = Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === '/api/users' && req.method === 'GET') {
      return Response.json([{ id: 1, name: 'Alex' }]);
    }

    if (url.pathname === '/api/users' && req.method === 'POST') {
      const body = await req.json();
      return Response.json({ id: 2, ...body }, { status: 201 });
    }

    return new Response('Not Found', { status: 404 });
  }
});

console.log(`Server running at http://localhost:${server.port}`);
Enter fullscreen mode Exit fullscreen mode

Built-in SQLite

import { Database } from 'bun:sqlite';

const db = new Database('myapp.sqlite');

// Create table
db.run(`CREATE TABLE IF NOT EXISTS users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  email TEXT UNIQUE
)`);

// Prepared statements (fast!)
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
insert.run('Alex', 'alex@example.com');

// Query
const users = db.prepare('SELECT * FROM users').all();
console.log(users);
Enter fullscreen mode Exit fullscreen mode

Built-in Test Runner

// math.test.ts
import { expect, test, describe } from 'bun:test';

describe('math', () => {
  test('addition', () => {
    expect(2 + 2).toBe(4);
  });

  test('async operations', async () => {
    const response = await fetch('http://localhost:3000/api/users');
    expect(response.status).toBe(200);
    const data = await response.json();
    expect(data).toHaveLength(1);
  });
});
Enter fullscreen mode Exit fullscreen mode
bun test  # Runs all .test.ts files
Enter fullscreen mode Exit fullscreen mode

File Operations (Fast)

// Write file
await Bun.write('output.json', JSON.stringify(data, null, 2));

// Read file
const content = await Bun.file('data.json').json();

// Stream large files
const file = Bun.file('large-video.mp4');
const stream = file.stream();
Enter fullscreen mode Exit fullscreen mode

Bun vs Alternatives

Feature Bun Node.js Deno
Speed 3-10x faster Baseline 1.5-2x faster
TypeScript Native ts-node Native
Package manager Built-in (npm compat) npm/yarn/pnpm npm compat
Test runner Built-in Jest/Vitest Built-in
Bundler Built-in Webpack/esbuild None
SQLite Built-in better-sqlite3 None
npm compatibility 95%+ 100% 90%+

Real-World Impact

A CI/CD pipeline ran npm install + jest tests + webpack build totaling 4 minutes per commit. After switching to Bun: bun install (8 seconds), bun test (12 seconds), bun build (3 seconds) = 23 seconds total. 90% faster CI, developers get feedback before their coffee gets cold.


Optimizing your JavaScript toolchain? I help teams migrate to faster runtimes. Contact spinov001@gmail.com or explore my data tools on Apify.

Top comments (0)