Bun

Bun v0.1.11

Jarred Sumner · September 7, 2022

To upgrade:

bun upgrade

To install:

curl https://bun.sh/install | bash
If you have any problems upgrading

Run the install script (you can run it multiple times):

curl https://bun.sh/install | bash

In Bun v0.1.11, web framework libraries built on Bun got faster

web frameworks

Benchmark: https://github.com/SaltyAom/bun-http-framework-benchmark This was run on Linux Image credit: @Kapsonfire-DE

Plugin API

Bun's runtime now supports a plugin API.

  • Import and require .svelte, .vue, .yaml, .scss, .less and other file extensions that Bun doesn't implement a builtin loader for
  • Dynamically generate ESM & CJS modules

The API is loosely based on esbuild's plugin API.

This code snippet lets you import .mdx files in Bun:

import { plugin } from "bun";
import { renderToStaticMarkup } from "react-dom/server";

// Their esbuild plugin runs in Bun (without esbuild)
import mdx from "@mdx-js/esbuild";
plugin(mdx());

// Usage
import Foo from "./bar.mdx";
console.log(renderToStaticMarkup(<Foo />));

This lets you import yaml files:

import { plugin } from "bun";

plugin({
  name: "YAML",

  setup(builder) {
    const { load } = require("js-yaml");
    const { readFileSync } = require("fs");
    // Run this function on any import that ends with .yaml or .yml
    builder.onLoad({ filter: /\.(yaml|yml)$/ }, (args) => {
      // Read the YAML file from disk
      const text = readFileSync(args.path, "utf8");

      // parse the YAML file with js-yaml
      const exports = load(text);

      return {
        // Copy the keys and values from the parsed YAML file into the ESM module namespace object
        exports,

        // we're returning an object
        loader: "object",
      };
    });
  },
});

We're planning on supporting browser builds with this plugin API as well (run at transpilation time)

Reliability improvements

Node compatibility:

  • feat: implement native os module by @xHyroM in #1115
  • fix buffer.copy by @zhuzilin in #1113
  • fix buffer.slice(0, 0) by @zhuzilin in #1114
  • Add buffer.indexOf, includes and lastIndexOf by @zhuzilin in #1112
  • Add native EventEmitter by @zhuzilin in #1123
  • Support emit Symbol events in EventEmitter by @zhuzilin in #1129
  • add SlowBuffer by @zhuzilin in #1133
  • update minified url polyfill by @samfundev in #1132
  • Add pad back to base64 by @zhuzilin in #1140
  • fix mkdtemp by @zhuzilin in #1151
  • Fix Buffer.isEncoding 39dc9899157d082841d79daa2f98dbecaa4efed6
  • Implement napi_add_finalizer f023b89b732db0aff24445acbbe39c366d13118d
  • NAPI_MODULE_INIT() wasn't implemented correctly in Bun and that has been fixed
  • import assert and import process did not behave as expected (assert wasn't returning a function). This has been fixed
  • "node:module"'s createRequire function wasn't requiring non-napi modules correctly

macOS event loop internals moved to a more reliable polling mechanism:

  • setTimeout CPU usage drops by 50% c1734c6 (Before: 90%, After: 33% - still more work to do here)
  • on macOS, bun would sometimes hang due to race conditions (unrelated to network connection) if you ran fetch enough times in quick succession. The race conditions have been fixed.

More:

  • [bun:ffi] Fix crash with uint64_t 296fb41e920736041c6c1dec38f1d8c355a402a1
  • [bun:ffi] Fix int16 / uin16 max 30992a8b051565ace57083b990d010316d56605d
  • Fix Request and Response in macros e0b35b3086b00fb27f950a72a082b360a3dad891
  • Fix clearTimeout on Linux e6a1209c53adb3056263b894d774b30ee70a3188

Performance improvements

Bun has a long-term commitment to performance. On macOS, React server-side rendering gets around 2x faster.

image

Coming up next in performance improvements: a new HTTP server implementation. Not far enough along for this release, but experiments are showing progress.

PRs

New Contributors

Full Changelog