Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Bun 29 min read Case study mediumAdept

Rewriting Bun in Rust

Bun, a 535k-line Zig JavaScript runtime, was mechanically ported to Rust over 11 days using about 50 continuously running Claude Code workflows. The motivation was a steady stream of use-after-free, double-free and leak bugs that came from mixing JavaScriptCore's garbage-collected values with manually managed Zig memory. The port was done all at once in a deliberately Zig-shaped style, guided by a porting guide and a lifetimes map, and validated against Bun's language-independent TypeScript test suite. Adversarial review in separate context windows caught bugs such as Box drops before async uv_close, negative-time timespec errors, and eager unwrap_or evaluation; cleanup toward idiomatic, less-unsafe Rust is planned after v1.4.

Related posts

  • Bun 1.4

    Bun mediumAdept

    Bun 1.4 adds 1,517 newly passing Node.js test-suite tests, fixes over 2,900 issues, and rewrites Bun from Zig to Rust. JavaScriptCore now uses an extended mimalloc instead of libpas, adding a scavenger thread, partial page clearing and better lazy zeroing. Together with GC timer tuning, segmented-array Strong roots and fewer futex calls, this cuts idle CPU 5x, HTTP server peak memory 13–48%, and Linux startup time in half. New APIs include Bun.Image, Bun.WebView, Bun.markdown, Bun.cron and Bun.Terminal, plus Markdown CPU, heap and bundle profiles and a cross-platform memoryPressure event built on kqueue, PSI and Windows resource notifications.

  • Bun v1.1.7

    Bun mediumAdept

    Bun v1.1.7 fixes 28 bugs and adds glob patterns in package.json workspaces (via Bun.Glob), asymmetric matcher support in expect.extend's this.equals, and a working bunx --version. Several fixes come with root causes: a SIMD invalid memory access in Zig's std.mem.indexOfScalarPos crashing Windows startup, a misassumed 8-byte alignment of FILE_DIRECTORY_INFORMATION, threadlocal destructors running during std::quick_exit with live Workers, an uninitialized C++ integer corrupting stacktrace line numbers, and a memory leak from unread Bun.serve() request bodies since v1.0.30. Smaller changes include WebSocket close code 1015 on TLS failures, a ServerWebSocket shrink from 32 to 24 bytes, and adding clang-analyzer to CI.

  • Bun v1.2.1

    Bun mediumAdept

    Bun v1.2.1 fixes 32 bugs and adds S3 storage class support, X25519 key generation in crypto.generateKeyPair, Node's --zero-fill-buffers flag, indented inline snapshots in bun test, and a bunfig option to disable minification for HTML imports. The most technical change fixes GC memory accounting: WebKit reports a ref-counted string's cost as its byte length divided by its reference count, so Bun's Zig bindings, which briefly held an extra reference, under-reported string memory (a 64 KB file read reported as 32 KB). A new single-step createUTF8ForJS pattern avoids that extra reference. Other fixes cover fs.Stats and bigint fstat parity with Node, EINTR retries in fs reads and writes, a child_process spawn race, Worker reliability, and memory leaks in Bun.serve() and Bun.pathToFileURL caused by refcount mistakes.

  • Bun v1.3.10

    Bun mediumAdept

    Bun v1.3.10 replaces its npm-based REPL with a native Zig REPL that supports top-level await, imports, history and tab completion. It also adds full TC39 stage-3 standard decorators, self-contained HTML output via `--compile --target=browser`, native Windows ARM64, and a global `--retry` flag for `bun test`. Bundler changes include barrel-file import optimization (automatic for `sideEffects: false` packages) and leaner module wrappers that cut 11% of heap objects on a large React app. Runtime changes include mTLS keepalive pooling, a JavaScriptCore upgrade (168x faster deep rope slicing, a faster `endsWith`), and structuredClone fast paths up to 25x faster for dense primitive arrays.

  • Bun v0.1.9

    Bun mediumAdept

    Bun v0.1.9 starts using JavaScriptCore's DOMJIT API to JIT-optimize native functions, making `ptr()` in `bun:ffi` 6-10x faster and `TextEncoder.encodeInto` 1.5-5x faster. To get there, Bun extended DOMJIT to accept Typed Array arguments and to declare more kinds of side effects. Bun now code-generates C++ binding classes and ABI-checked Zig bindings from JavaScript class definitions, an approach inspired by WebIDL, replacing most use of the JSC C API. Other changes include a more reliable, lower-CPU event loop on Linux, about 20% faster `Bun.serve` through better `Headers` handling, native `buffer` and `process` modules, and numerous crash fixes.

The quest for software engineering knowledge.

Look:
Layout: