10 KiB
Bun is a new JavaScript & TypeScript runtime designed to be a faster, leaner, and more modern drop-in replacement for Node.js.
Speed
Bun is designed to start fast and run fast. Its transpiler and runtime are written in Zig, a modern, high-performance language. On Linux, this translates into startup times 4x faster than Node.js.
{% image src="/images/bun-run-speed.jpeg" caption="Bun vs Node.js vs Deno running Hello World" /%}
Performance sensitive APIs like Buffer, fetch, and Response are heavily profiled and optimized. Under the hood Bun uses the JavaScriptCore engine, which is developed by Apple for Safari. It starts and runs faster than V8, the engine used by Node.js and Chromium-based browsers.
TypeScript
Bun natively supports TypeScript out of the box. All files are transpiled on the fly by Bun's fast native transpiler before being executed. Similar to other build tools, Bun does not perform typechecking; it simply removes type annotations from the file.
$ bun index.js
$ bun index.jsx
$ bun index.ts
$ bun index.tsx
Some aspects of Bun's runtime behavior are affected by the contents of your tsconfig.json file. Refer to Runtime > TypeScript page for details.
JSX
JSON and TOML
Source files can import a *.json or *.toml file to load its contents as a plain old JavaScript object.
import pkg from "./package.json";
import bunfig from "./bunfig.toml";
WASI
{% callout %} 🚧 Experimental {% /callout %}
Bun has experimental support for WASI, the WebAssembly System Interface. To run a .wasm binary with Bun:
$ bun ./my-wasm-app.wasm
# if the filename doesn't end with ".wasm"
$ bun run ./my-wasm-app.whatever
{% callout %}
Note — WASI support is based on wasi-js. Currently, it only supports WASI binaries that use the wasi_snapshot_preview1 or wasi_unstable APIs. Bun's implementation is not fully optimized for performance; this will become more of a priority as WASM grows in popularity.
{% /callout %}
Node.js compatibility
Long-term, Bun aims for complete Node.js compatibility. Most Node.js packages already work with Bun out of the box, but certain low-level APIs like dgram are still unimplemented. Track the current compatibility status at Ecosystem > Node.js.
Bun implements the Node.js module resolution algorithm, so dependencies can still be managed with package.json, node_modules, and CommonJS-style imports.
{% callout %} Note — We recommend using Bun's built-in package manager for a performance boost over other npm clients. {% /callout %}
Web APIs
Some Web APIs aren't relevant in the context of a server-first runtime like Bun, such as the DOM API or History API. Many others, though, are broadly useful outside of the browser context; when possible, Bun implements these Web-standard APIs instead of introducing new APIs.
The following Web APIs are partially or completely supported.
{% table %}
- URLs
URLURLSearchParams
- Streams
ReadableStreamWritableStreamTransformStreamByteLengthQueuingStrategyCountQueuingStrategyand associated classes
- Blob
Blob
- WebSockets
WebSocket
- Encoding and decoding
atobbtoaTextEncoderTextDecoder
- Timeouts
setTimeoutclearTimeout
- Intervals
setIntervalclearInterval
- Crypto
cryptoSubtleCryptoCryptoKey
-
Debugging
- Microtasks
queueMicrotask
- Errors
reportError
- Realms
ShadowRealm
{% /table %}
Bun APIs
Bun exposes a set of Bun-specific APIs on the Bun global object and through a number of built-in modules. These APIs represent the canonical "Bun-native" way to perform some common development tasks. They are all heavily optimized for performance. Click the link in the left column to view the associated documentation.
{% table %}
- Topic
- APIs
- HTTP
Bun.serve
- File I/O
Bun.fileBun.write
- Processes
Bun.spawnBun.spawnSync
- TCP
Bun.listenBun.connect
- Transpiler
Bun.Transpiler
- Routing
Bun.FileSystemRouter
- HTMLRewriter
HTMLRewriter
- Utils
Bun.peekBun.which
- SQLite
bun:sqlite
- FFI
bun:ffi
- DNS
bun:dns
- Testing
bun:test
- Node-API
Node-API
{% /table %}
Plugins
Support for additional file types can be implemented with plugins. Refer to Runtime > Plugins for full documentation.