Skip to content

Bun 2.0 New Features and Improvements

Recently, our team has rolled out Bun 2.0 New Features and Improvements and accumulated a lot of experience. I've compiled it here for reference, hoping it helps others doing similar work.

Core Concepts

We can improve it in the following ways:

javascript
const fs = require("fs");
const { Transform, pipeline } = require("stream");
const { promisify } = require("util");
const pipelineAsync = promisify(pipeline);

const csvToJson = new Transform({
  transform(chunk, encoding, callback) {
    const lines = chunk.toString().split("\n");
    const headers = lines[0].split(",");
    for (let i = 1; i < lines.length; i++) {
      if (!lines[i].trim()) continue;
      const values = lines[i].split(",");
      const obj = {};
      headers.forEach((h, idx) => (obj[h.trim()] = values[idx]?.trim()));
      this.push(JSON.stringify(obj) + "\n");
    }
    callback();
  },
});

This solution has been running stably in production for over six months and has been validated in practice.

Deep Dive

Let's start with the basic implementation:

javascript
const express = require("express");
const app = express();

app.use(express.json());

class AppError extends Error {
  constructor(status, message) {
    super(message);
    this.statusCode = status;
  }
}

const asyncHandler = (fn) => (req, res, next) =>
  Promise.resolve(fn(req, res, next)).catch(next);

app.get(
  "/api/users/:id",
  asyncHandler(async (req, res) => {
    const user = await User.findById(req.params.id);
    if (!user) throw new AppError(404, "User not found");
    res.json({ data: user });
  }),
);

This code demonstrates the basic usage. In real projects, you also need to consider error handling and edge cases.

Practical Experience

Building on this foundation, we can further optimize:

javascript
const fs = require("fs");
const { Transform, pipeline } = require("stream");
const { promisify } = require("util");
const pipelineAsync = promisify(pipeline);

const csvToJson = new Transform({
  transform(chunk, encoding, callback) {
    const lines = chunk.toString().split("\n");
    const headers = lines[0].split(",");
    for (let i = 1; i < lines.length; i++) {
      if (!lines[i].trim()) continue;
      const values = lines[i].split(",");
      const obj = {};
      headers.forEach((h, idx) => (obj[h.trim()] = values[idx]?.trim()));
      this.push(JSON.stringify(obj) + "\n");
    }
    callback();
  },
});

This pattern is very practical in large projects and can significantly reduce maintenance costs.

Tuning Strategies

In real projects, the usage gets more complex:

javascript
const express = require("express");
const app = express();

app.use(express.json());

class AppError extends Error {
  constructor(status, message) {
    super(message);
    this.statusCode = status;
  }
}

const asyncHandler = (fn) => (req, res, next) =>
  Promise.resolve(fn(req, res, next)).catch(next);

app.get(
  "/api/users/:id",
  asyncHandler(async (req, res) => {
    const user = await User.findById(req.params.id);
    if (!user) throw new AppError(404, "User not found");
    res.json({ data: user });
  }),
);

MIT Licensed