Read
Edit
History
Notify
Share
Bun JavaScript Runtime
Bun
Bun is a fast all-in-one JavaScript runtime and toolkit designed to develop, test, run, and bundle JavaScript and TypeScript projects with a focus on speed and developer experience. It aims for 100% Node.js compatibility while providing enhanced performance and additional built-in features.
Overview
Launched as an alternative to established JavaScript runtimes like Node.js and Deno, Bun extends the JavaScriptCore engine (from WebKit/Safari) with native-speed functionality implemented in Zig. This architecture choice contributes significantly to Bun's performance advantages, particularly in startup times and execution speed.
Bun positions itself as a complete toolkit for JavaScript development, combining a runtime environment with a package manager, test runner, and bundler in a single cohesive product. This integrated approach aims to reduce complexity and improve developer productivity by eliminating the need for multiple separate tools.
The project's core design goals include superior speed, elegant APIs, and a cohesive developer experience. Beyond just running JavaScript, Bun provides highly-optimized APIs for common tasks like HTTP servers, file operations, and database access, making it particularly suitable for server-side applications.
Key Features
Runtime Capabilities
- Node.js Compatibility: Bun implements Node's module resolution algorithm, globals like Buffer and process, and built-in modules like fs and path, aiming to be a drop-in replacement [1]
- Web Standard APIs: Native support for modern web APIs including fetch, URL, EventTarget, Headers, and more, powered by WebCore from WebKit/Safari [1]
- TypeScript and JSX Support: First-class support for TypeScript and JSX without additional configuration, respecting tsconfig.json settings including "paths", "jsx", and other configurations [1]
- Module Support: Simplified module handling that works with both CommonJS and ESM without complicated configuration rules [1]
Built-in APIs
- HTTP and WebSocket Server: Native HTTP, HTTPS, and WebSocket server implementation via
Bun.serve()
[1] - Database Drivers: Built-in high-performance drivers for PostgreSQL and SQLite [1]
- S3 Cloud Storage: Native S3-compatible object storage client [1]
- HTTP Router: Built-in routing with support for dynamic and wildcard routes [1]
- Single-file Executables: Ability to bundle applications into single executable files for easy deployment [1]
Developer Tooling
- Package Manager: npm-compatible package manager that's significantly faster than npm, yarn, and pnpm [1]
- Test Runner: Jest-compatible test runner with support for lifecycle hooks, snapshot testing, and DOM APIs [1]
- Bundler: Built-in bundler for creating production-ready code for both frontend and backend [1]
- Shell API: Cross-platform shell scripting via
Bun.$
API [1] - Hot Reloading: Server hot reloading that preserves connection state [1]
- Monorepo Support: Built-in support for workspaces and monorepo management [1]
Utility APIs
- Password & Hashing: Built-in APIs for bcrypt, argon2, and non-cryptographic hash functions [1]
- String Width: Calculate the display width of strings in terminals [1]
- Glob Patterns: Native support for file matching with glob patterns [1]
- Semver: Compare and sort semantic version strings [1]
- CSS Color Conversion: Convert between CSS color formats [1]
- Foreign Function Interface: Call C-compatible native code from JavaScript [1]
Performance
Bun demonstrates significant performance advantages over Node.js and Deno in various benchmarks:
HTTP Performance
- Express.js 'hello world': Bun handles 59,026 requests per second compared to Node.js at 19,039 and Deno at 25,335 requests per second [1]
WebSocket Performance
- WebSocket chat server: Bun can process 2,536,227 messages per second with 32 clients, compared to Node.js at 435,099 and Deno at 1,320,525 messages per second [1]
Database Performance
- PostgreSQL queries: Bun can execute 50,251 queries per second compared to Node.js at 14,398 and Deno at 11,821 queries per second [1]
Package Management
Bun's package manager is significantly faster than alternatives:
- 17x faster than pnpm
- 29x faster than npm
- 33x faster than Yarn (When installing dependencies from cache for a Remix app) [1]
Package Manager
Bun includes an npm-compatible package manager that offers several advantages:
- Node.js Compatibility: Installs dependencies into node_modules like npm and other package managers [1]
- Performance: Uses the fastest system calls available on each operating system for superior installation speed [1]
- Workspaces Support: Built-in support for monorepo workspaces [1]
- Global Install Cache: Downloads packages once and reuses them across projects [1]
- Security Features: Doesn't execute postinstall scripts by default, with an allowlist system for trusted dependencies [1]
- Cross-platform Scripts: Package.json scripts are powered by Bun Shell, eliminating the need for utilities like cross-env or rimraf [1]
- Compatibility: Reads .npmrc and package-lock.json files for easy migration from npm [1]
Test Runner
Bun's test runner provides a fast alternative to Jest and other testing frameworks:
- Jest Compatibility: Provides a Jest-style expect() API, allowing users to switch to Bun with minimal code changes [1]
- Performance: Significantly faster than alternatives like Jest+Babel (20x slower), Jest+tsjest (18x slower), Jest+SWC (8x slower), and Vitest (5x slower) [1]
- Modern Features: Support for ESM, TypeScript, JSX, snapshot testing, DOM APIs, watch mode, and function mocks [1]
Industry Adoption
Bun has gained adoption in production environments, including at major technology companies:
- X (formerly Twitter): "Bun is at the heart of one of our newest infrastructure projects [...] in production, powering our sports and upcoming news products" - Shlomi Atar, Engineering Team at X [1]
Comparison with Other Runtimes
Bun vs. Node.js
- Performance: Bun generally outperforms Node.js in HTTP requests, WebSocket messaging, and database operations [1]
- Built-in Features: Bun includes many features that require third-party packages in Node.js, such as TypeScript support, bundling, and testing [1]
- Package Management: Bun's package manager is significantly faster than npm [1]
- Compatibility: Bun aims for 100% Node.js compatibility, allowing existing Node.js applications to run with minimal changes [1]
Bun vs. Deno
- Performance: Bun outperforms Deno in most benchmarks, including HTTP, WebSocket, and database operations [1]
- npm Ecosystem: Unlike Deno's initial approach, Bun embraces the npm ecosystem with its fast package manager [1]
- Built-in Features: Both runtimes offer built-in features like TypeScript support, but Bun adds package management, bundling, and more [1]
Installation
Bun can be installed on various platforms:
Linux & macOS
curl -fsSL https://bun.sh/install | bash
Windows
powershell -c "irm bun.sh/install.ps1 | iex"
Usage Examples
HTTP Server
const server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on localhost:${server.port}`);
WebSocket Server
const server = Bun.serve<{ authToken: string; }>({
fetch(req, server) {
const cookies = parseCookies(req.headers.get("Cookie"));
server.upgrade(req, {
data: { authToken: cookies['X-Token'] },
});
},
websocket: {
async message(ws, message) {
console.log(`Received: ${message}`);
const user = getUserFromToken(ws.data.authToken);
await db.Message.insert({
message: String(message),
userId: user.id,
});
},
},
});
File Operations
const file = Bun.file(import.meta.dir + '/package.json');
const pkg = await file.json();
pkg.name = 'my-package';
pkg.version = '1.0.0';
await Bun.write(file, JSON.stringify(pkg, null, 2));
Database Query (SQLite)
import { Database } from "bun:sqlite";
const db = new Database("db.sqlite");
console.log(db.query("SELECT 1 as x").get());
// { x: 1 }
Current Status
As of April 2024, Bun is at version 1.2.10, with ongoing development focused on improving Node.js compatibility, performance, and adding new features. The project has gained significant traction in the JavaScript community, with many developers adopting it for both development and production environments.
Bun JavaScript Runtime
Commit Info
Edited By
Edited On
April 24, 2025
Reason for edit:
"Republishing the Bun JavaScript Runtime wiki with updated content and events."
Feedback
Average Rating
How was your experience?
Give this wiki a quick rating to let us know!
Media
REFERENCES
[1]