Table of Contents
Introduction
Bun was formally released in version 1.0 and is now available for use.
This is a major turning point for the TypeScript/JavaScript ecosystem.
Bun simplifies development like never before by combining a bundler, package manager, and runtime environment.
Here is the Bun 1.0:
See also https://bun.sh/blog/bun-v1.0”
Important insights
- Bun performs much faster than npm; on a MacBook Pro, a script starts in 30 milliseconds whereas npm takes 150 milliseconds.
- Bun is a versatile tool for a range of development needs because of its plugin API, which lets developers design custom loaders and expand the runtime to include other capabilities like importing YAML files.
- Bun server is a strong option for creating high-performance HTTP servers because it can process four times as many requests per second as the same node.js server.
- Bun approaches node.js compatibility economically, focusing on APIs that are truly helpful to developers. This guarantees that 99% of full-stack apps, including those created with popular frameworks, will function without any issues.
- Workspaces, custom registries, git dependencies, and local package linking are just a few of the capabilities that Bun package manager provides.
- Bun's expect.2 equal implementation is a hundred times faster than Jest's, giving developers a quicker and more optimized experience.
- Bun 1.0 is only the beginning; they intend to hire entry-level system engineers and engage community support to help them expand and improve even more.
IoT, Edge Software, and Cloud Features
Bun's dedication to reducing resource consumption is one of its unique selling points. This makes it especially desirable for applications involving edge devices, cloud services, and the Internet of Things (IoT).
Resource efficiency is crucial in many sectors, and Bun is a game-changer due to its ability to give great performance while using low resources.
Moreover, Bun's careful fusion of WebAssembly and Rust adds even more allure, especially for edge developers.
Its versatility is further enhanced by this strategic combination, which also highlights its dedication to remaining at the forefront of cutting-edge technology.
Bun is a complete set of tools.
We like JavaScript. It has a strong developer community, is well-established, and moves quickly. It is fantastic.
But layers upon layers of tooling have been built since Node.js first appeared, 14 years ago. JavaScript tooling has also grown cumbersome and slow, as with any system that expands and changes without centralized planning.
Why Bun is in Existence
Bun wants to get rid of all the complexities and delays without sacrificing any of the wonderful things about JavaScript. The conventions you are accustomed to should not require you to relearn them, and your preferred libraries and frameworks ought to continue to function.
However, you will have to unlearn the numerous tools that Bun eliminates from need:
- Bun will be used as a drop-in replacement for Node.js, so the following is not needed:
node
npx — bunx
is 5x faster
Dotenv
cross-env
Bun reads .env
files by default
nodemon
pm2
built-in watch mode
ws
— built-in WebSocket serve
- Bun can execute files beginning
in.js
.ts
.cjs
.mjs
.jsx
and.tsx
which can be substituted for:
tsc
— (but you can keep it for typechecking!)
babel
.babelrc
@babel/preset-*
ts-node
ts-node-esm
tsx
- Bundlers: Bun is a JavaScript bundler that has best-in-class performance and a plugin API that works with esbuild, so it eliminates the need for:
esbuild
webpack
parcel
.parcelrc
rollup
rollup.config.js
- Bun is package management that is compatible with npm and has recognized commands. It reads your
package.json
and writes tonode_modules
just like other package managers, so you can replace:
npm
.npmrc
package-lock.json
yarn
yarn.lock
pnpm
pnpm.lock
pnpm-workspace.yaml
lerna
- Testing libraries — Bun supports snapshot testing, mocking, and code coverage, so you don't need:
jest
jest.config.js
ts-jest
@swc/jest
babel-jest
jest-extended
vitest
vitest.config.ts
Even while these tools are generally good on their own, combining them all at once leads to fragility and a sluggish development experience. They carry out a lot of repetitive work—your code will be parsed by many tools three or more times when you use jest! Additionally, the duct tape, plugins, and adapters needed to secure everything together ultimately tear.
Bun avoids these integration issues with a single integrated toolbox. From performance to API design, every tool in this toolkit offers the greatest developer experience available.
Bun APIs
- Bun includes highly optimized standard-library APIs for the majority of developer needs.
- Bun-native APIs are intended to be quick and easy to use, like Node.js APIs, which are there for backward compatibility.
Bun.file()
To casually load a Bun.file()
file at a certain path, use Bun.file().
const file = Bun.file("package.json");
const contents = await file.text();
The BunFile that is returned is an extension of the Web standard File. Different formats of the file contents might be slowly loaded.
const file = Bun.file("package.json");
await file.text(); // string
await file.arrayBuffer(); // ArrayBuffer
await file.blob(); // Blob
await file.json(); // {...}
Bun.write()
Make use of A single, flexible API called Bun.write()
can be used to write practically anything on a disc, including strings, binary data, blobs, and even Response objects.
await Bun.write("index.html", "<html/>");
await Bun.write("index.html", Buffer.from("<html/>"));
await Bun.write("index.html", Bun.file("home.html"));
await Bun.write("index.html", await fetch("https://example.com/"));
Bun.serve()
To spin up a WebSocket server, an HTTP server, or both, use Bun.serve()
. It is built upon well-known Web-standard APIs, such as Request
and Response
.
Bun.serve({
port: 3000,
fetch(request) {
return new Response("Hello from Bun!");
},
});
You can also configure TLS using the tls option
Just build an event handler inside of the WebSocket to support WebSockets in addition to HTTP. In contrast, Node.js requires a third-party requirement called ws and lacks an integrated WebSocket API.
Bun.serve({
fetch() { ... },
websocket: {
open(ws) { ... },
message(ws, data) { ... },
close(ws, code, reason) { ... },
},
});
Bun:sqlite
Bun comes with built-in SQLite support. Its native code API is designed to be quicker and draws inspiration from better-sqlite3.
import { Database } from "bun:sqlite";
const db = new Database(":memory:");
const query = db.query("select 'Bun' as runtime;");
query.get(); // => { runtime: "Bun" }
Bun.password
Also, Bun includes APIs for common yet sophisticated things that you wouldn't want to create on your own.
With bcrypt or argon2, you can hash and validate passwords with Bun.password no additional dependencies are needed.
const password = "super-secure-pa$$word";
const hash = await Bun.password.hash(password);
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh...
const isMatch = await Bun.password.verify(password, hash);
// => true