Bun, the modern JavaScript runtime and toolkit, has released version 1.2 today with a host of exciting features and enhancements. Designed to streamline building, running, and testing JavaScript and TypeScript applications, this release focuses on improving stability, enhancing Node.js compatibility, and introducing powerful new APIs. Here's a detailed look at what's new in Bun 1.2.
More Comprehensive Support for Node.js APIs: Bun Passes 90% of the Node.js Test Suite#
One of Bun's key goals is to serve as a drop-in replacement for Node.js, while also offering significant performance improvements and additional features. In version 1.2, Bun extends its support for Node.js APIs by adding compatibility for the following:
node:http2 server
node:dgram
node:cluster
These additions bring Bun closer to full Node.js parity, allowing developers to use Bun in applications that rely on these APIs.
Performance is a major focus when Bun adds support for new APIs. The development team’s tests of a "Hello World" Express server running on Bun 1.2 showed that it can handle 3X more requests per second compared to Node.js. To ensure reliability, Bun now runs the entire Node.js test suite for every change, which includes thousands of tests from Node’s repository. Currently, Bun passes over 90% of Node’s test suite for supported modules, with ongoing progress toward full compatibility.
New Built-in S3 Object Support#
Bun 1.2 introduces built-in support for S3 object storage, a standard for cloud-based file systems. The new Bun S3 API allows developers to seamlessly read and write files in S3 buckets using the same straightforward APIs as Bun’s file operations.
Key features include:
- Integration with web standard APIs, allowing S3 objects to be used as blobs in requests and responses.
- Support for well-known environment variables to simplify configuration.
- A highly optimized native implementation for faster file downloads, achieving speeds up to 5x faster than leading npm packages in Node.js.
This feature enhances Bun's appeal as a cloud-first runtime and makes it a more compelling choice for developers building scalable, cloud-native applications.
New Built-in Postgres Support#
In addition to S3, Bun 1.2 introduces built-in Postgres support with the new Bun SQL API. The new Bun.sql SQL client supports PostgreSQL and will soon add MySQL.
import { sql } from "bun";
const users = await sql`
SELECT * FROM users
WHERE active = ${true}
LIMIT ${10}
`;
// Select with multiple conditions
const activeUsers = await sql`
SELECT *
FROM users
WHERE active = ${true}
AND age >= ${18}
`;
Highlights include:
- Tagged template literals for creating queries, preventing SQL injection by design.
- APIs inspired by the popular postgres.js npm package, allowing for easy migration by updating the import statement.
- Significant performance improvements, with row reading up to 50% faster than leading npm packages in Node.js.
Bun's New Text-Based Lockfile#
Bun 1.2 addresses feedback about its binary lockfile by introducing a new text-based lockfile in JSONC format. This makes lockfiles easier to read, review in pull requests, and resolve merge conflicts. While maintaining performance, cached installs are now 30% faster, and Bun install defaults to generating a text lockfile for new projects.
For existing projects, migration to the text lockfile can be done by using the --save-text-lockfile
flag. (Bun plans to support the binary lockfile “for a long time” so users can continue using common commands as expected.)
Bun Introduces HTML Imports#
Simplifying front-end development, Bun 1.2 introduces HTML Imports. This feature allows developers to import HTML files directly, with automatic bundling and minification of JavaScript and CSS. Frameworks like React, TypeScript, and Tailwind CSS work seamlessly with HTML Imports, streamlining full-stack application development.
Bun 1.2 Is a Major Milestone for Bun’s Versatility and Adoption#
The Bun project has gained significant traction in the JavaScript community as a potential successor or alternative to Node.js, especially for developers prioritizing performance and ease of use. Bun shows a 16% adoption rate among respondents to the 2024 State of JavaScript survey, showing a lot of growth potential given that it’s a relatively new runtime compared to Node.js and Deno.
By introducing built-in support for critical datastores like S3 and Postgres, Bun takes a major step towards meeting the demands of production applications, enabling developers to build scalable, cloud-native solutions with fewer external dependencies. These new features in Bun 1.2 go a long way towards solidifying Bun’s position as a mainstream JavaScript runtime. Check out the Bun 1.2 announcement post for implementation details on all the new features and a full breakdown of everything in the release.