github oven-sh/bun bun-v0.0.68
bun v0.0.68

latest releases: bun-v1.1.8, bun-v1.1.7, bun-v1.1.6...
2 years ago

To upgrade:

bun upgrade

bun v0.0.68

This release is mostly focused on bun.js, bun's JavaScript runtime environment. Fixes to bun install will be part of the next release, but the infrastructure work from this release will help with bun install in the next release.

TLDR:

  • Bun.Transpiler lets you run Bun's JS/TSX transpiler programmatically from bun.js
  • bun.js natively implements Node.js' fs module (sync functions), and its fast
  • bun.js has more support for the Node.js process object

Bun.Transpiler - API access to Bun

const bun = new Bun.Transpiler({
  loader: "tsx", // set default loader
});

// logs transpiled code without resolving imports
console.log(bun.transformSync("export default <div />"));

// return list of imports & exports without resolving imports or printing code
const { imports, exports } = bun.scan(`
  import { Component } from "react";
  export const foo: boolean = true;
`);

console.log({ exports, imports });

Bun.Transpiler exposes part of Bun's JavaScript & TypeScript transpiler from native code to JavaScript, and it's fast.

End-to-end, transpiling this JSX file inside JavaScript via Bun.Transpiler runs:

  • 8x faster than swc
  • 15x faster than esbuild
  • 40x faster than babel

image

See benchmark code

Bun.Transpiler supports JavaScript plugins with AST access via macros. Macros are not entirely done yet, but simple ones work. There will be docs on this.

Bun.Transpiler is builtin to bun.js, there's nothing extra to import or install. Eventually there will be a WASM build for some of this, but not sure when

However, a transpiler API is not very useful without a way to read/write files to disk.

Node.js fs module implementation

// This works in bun.js now
import {readFileSync} from 'fs';

// require("fs") also works in both .mjs files and .js/.cjs files
require("fs").writeFileSync("foo.txt", "bar!")

// you can also use the node: namespace if you want
import {readlinkSync} from 'node:fs';

You can now use most of the sync functions from Node.js' fs module inside bun.js. These are implemented from scratch in Zig & exposed to JS. Buffer & streams are not implemented yet, but you can pass a Uint8Array or ArrayBuffer where Node accepts a Buffer. The async versions of the functions will come in a future release (this already was a lot of stuff for one release), but generally sync outperforms async for local file access.

fs.realpathSync is about 7x faster in bun.js (50,000 iterations)

image

fs.existsSync runs about 30% faster in bun.js (100,000 iterations)

image

The following functions are implemented:

  • fs.accessSync
  • fs.appendFileSync
  • fs.chmodSync
  • fs.chownSync
  • fs.closeSync
  • fs.copyFileSync
  • fs.existsSync
  • fs.fchmodSync
  • fs.fchownSync
  • fs.fstatSync
  • fs.fsyncSync
  • fs.ftruncateSync
  • fs.futimesSync
  • fs.lchmodSync
  • fs.lchownSync
  • fs.linkSync
  • fs.lstatSync
  • fs.lutimesSync
  • fs.mkdirSync
  • fs.openSync
  • fs.readdirSync
  • fs.readFileSync
  • fs.readlinkSync
  • fs.readSync
  • fs.realpathSync
  • fs.renameSync
  • fs.statSync
  • fs.symlinkSync
  • fs.truncateSync
  • fs.unlinkSync
  • fs.utimesSync
  • fs.writeFileSync
  • fs.writeSync

Bun also includes an implementation of Node's SystemError with pretty printing. Note that since source maps are not implemented yet, sometimes the line:column will be off by a little.

image

This is what the same error looks like in Node

image

Node.js process object

bun.js has more support for the process object from Node.js.

// These work now in bun.js
process.chdir("insert-dir-name-here");
process.cwd();
// arguments used to launch, excluding "run" if via "bun run" for compatibility with npm packages
process.argv;
// current process id
process.pid;
// parent process pid
process.ppid;

// returns bun's version
process.version

process.versions
{
  // fake version for compatibility with npm packages potentially looking this up
  "node": "17.0.0",
  "modules": "67",
  // bun version
  "bun": "0.0.68",
  // git shas/tag of bun dependencies
  "webkit": "96e77eccfde8dc9c207520d8ced856d8bdb8d386",
  "mimalloc": "f412df7a2b64421e1f1d61fde6055a6ea288e8f5",
  "libarchive": "dc321febde83dd0f31158e1be61a7aedda65e7a2",
  "picohttpparser": "066d2b1e9ab820703db0837a7255d92d30f0c9f5",
  "boringssl": "b3ed071ecc4efb77afd0a025ea1078da19578bfd",
  "zlib": "959b4ea305821e753385e873ec4edfaa9a5d49b7",
  "zig": "0.10.0-dev.315+4d05f2ae5"
}

// process.nextTick() is now implemented (currently only supports up to 4 arguments)
process.nextTick(() => console.log("second"));
console.log("first");

More stuff

import.meta in bun.js returns an object with file and dir

const {
  // import.meta.dir returns absolute path to the directory the script is in. sort of like __dirname
  dir,
  // import.meta.file returns absolute path to the script
  file,
} = import.meta;
  • queueMicrotask is implemented
  • If bun exits due to running out of file descriptors, bun will print some platform-specific instructions explaining how to fix it.
  • No longer need a ./ to run a script with bun.js, e.g. instead of bun ./foo.js, bun foo.js works now
  • Bun actually detects .mjs or .mts files now and treats them as ESM. Before it was reading them but ignoring the extension
  • Fixed a couple regressions that caused bun to start up slower, now it's back to about 3.5ms on macOS aarch64 and 0.5ms on linux amd64
  • Fixed a bug in the upgrade checker that would sometimes cause bun to crash, typically after about 30 seconds
  • Bun.gc(force) lets you manually run the garbage collector
  • Bun.shrink() runs a JavaScriptCore VM function that attempts to shrink the amount of memory used by JavaScriptCore
  • Bun.generateHeapSnapshot() returns a heap snapshot in a format I'm not entirely sure how to visualize yet

If you look hard enough, you'll also find a new subcommand for a very incomplete but fast Jest-like test runner. Hopefully will talk more about that next release or the one after.

Thanks

Don't miss a new bun release

NewReleases is sending notifications on new releases.