Bun Rewrites in Rust: What It Means for the JavaScript Runtime

· 7 min read · Review

Bun 1.4 is the first version of the runtime written in Rust. What the million-line port changed, what the benchmarks show when read correctly, and why Bun's team left Zig.

Bun Rewrites in Rust: What It Means for the JavaScript Runtime

Bun 1.4, released on 20 August 2026, is the first version of the JavaScript runtime written in Rust rather than Zig (Bun). The port behind it was merged three months earlier as a single pull request that added 1,009,257 lines across 6,755 commits.

This post first appeared on 24 May 2026, ten days after that merge. It was revised on 8 November 2026 to add the release, Bun’s own account of why it left Zig, and corrected benchmark figures: the first version misread the columns of the benchmark table it quoted. Every figure below was checked against the pull requests through the GitHub API, and against Bun’s blog, on 8 November 2026. It is written for teams that run Bun in production or are choosing a JavaScript runtime, and for anyone considering an AI-assisted port of a large codebase.

What was merged

The rewrite landed as one large port and four follow-up pull requests, all opened and merged by Bun’s creator, Jarred Sumner.

Pull requestOpenedMergedCommitsLines added / removed
#30412 Rewrite Bun in Rust8 May14 May6,755+1,009,257 / −4,024
#30875 Fix effectively every native-code memory leak16 May19 May251+5,004 / −1,822
#30901 Bundler and transpiler hot-path optimisations17 May17 May34+408 / −256
#31115 Close the main-thread perf gap vs Zig 1.3.1420 May20 May18+1,324 / −1,112
#31116 Clippy deny lints and 2,735 fixes20 May20 May53+18,626 / −23,043
Total8 May20 May7,111+1,034,619 / −30,257
gantt
    title Bun Rust rewrite, May 2026 (opened to merged)
    dateFormat YYYY-MM-DD
    axisFormat %d %b
    section Port
    30412 rewrite, 6755 commits : 2026-05-08, 2026-05-14
    section Follow-up
    30875 memory leaks : 2026-05-16, 2026-05-19
    30901 hot paths : 2026-05-17, 1d
    31115 main-thread gap : 2026-05-20, 1d
    31116 clippy sweep : 2026-05-20, 1d

The whole sequence took 12 days. The description of the main pull request sets out the result in Sumner’s words:

It passes Bun’s pre-existing test suite on all platforms (and fixes several memory leaks and flaky tests), the binary size shrinks by 3 MB - 8 MB, the benchmarks are between neutral and faster - and most importantly, we now have compiler-assisted tools for catching & preventing memory bugs, which have costed the team an enormous amount of development & debugging time over the years.

It also describes how conservative the port was: “The same architecture, the same data structures. Bun still uses few 3rd party libraries. No async rust.”

Why Bun left Zig

Sumner explained the decision at length on 8 July (Bun):

Our bugfix list felt bad and I was tired of going to sleep worrying about crashes in Bun. I don’t blame Zig for that - other users of Zig don’t have the bugs we had, and mixing GC with manually-managed memory is an uncommon enough thing for software to need that no language really designs for it.

The bugs were of a specific kind:

A large percentage of bugs from that list are use-after-free, double-free, and “forgot to free” in an error path. In safe Rust, these are compiler errors and RAII-like automatic cleanup with Drop.

He was also clear about the cost of the alternative. “Historically, rewrites are a terrible idea. Excluding comments, Bun is 535,496 lines of Zig. A rewrite in another language would take a small team of engineers a full year.”

The argument is narrower than “Rust is safer than Zig”. Bun runs a garbage-collected JavaScript engine inside native code that manages its own memory, and its bugs came from the boundary between the two. That is a statement about Bun’s design, which is why it does not transfer automatically to other Zig projects, and Sumner says as much.

The performance numbers, read correctly

PR #31115 is the source for the bundler comparison with Zig. Its benchmark ran on the rolldown-benchmark suite on a 64-core Linux machine. Its table has four columns, and they are easy to misread; the first version of this post did.

Build100k files, wall time100k files, main thread10k files, wall timeTotal CPU vs Zig
Rust, before #311153,819 ms3,065 ms470 ms−29%
Rust, after #311153,443 ms~2,680 ms429 ms−41%
Zig 1.3.143,398 ms2,417 ms422 msbaseline

According to the same pull request, after the fixes the Rust build was 1.3% slower than Zig on 100k files (3,443 against 3,398 ms) and 1.7% slower on 10k files (429 against 422 ms), while using 41% less CPU in total. The pull request explains why both can be true: “The wall-clock gap was entirely main-thread serialization during the parse-result loop and link phase.” The port spreads more of its work across cores, so it does less work in total, but the wall time still waits on the one thread that cannot be parallelised. The “1.05× faster” figure in the same pull request compares the branch with Bun’s own canary build at the time, not with Zig.

Three of the fixes show what a mechanical port gets wrong:

  • 48 spurious #[cold] hints. The port marked every public method in the bundler’s main file as cold, which tells the compiler to deprioritise it; the Zig code had none. They were slowing the hot path.
  • A smaller Path. Storing a parsed path name inline made the struct 120 bytes; computing it on demand cut it to 56.
  • About 1.1 million allocation and free pairs removed by iterating by index instead of cloning a vector for each part.

The pull request puts the remaining gap at about 250 ms on the main thread at 100k files, spread across functions that each take under 2% of the time and are “largely cache-miss-bound”.

Memory leaks the port made visible

PR #30875 made Bun’s native code, in its own words, “effectively leak-free”: the entire test suite now runs under LeakSanitizer in CI, and every leak it reported was fixed.

The cause was in the build, not in Rust. The Zig build used plain libc malloc in its sanitiser builds, which LeakSanitizer can see. The port made the faster mimalloc allocator unconditional, and in that configuration “most leaks were simply invisible”. The fix restores libc allocation for sanitiser builds only. Once the detector could see the allocations, the leaks it found were fixed in the same pull request.

The lint sweep and how it was run

PR #31116 turned on a set of deny-level lints from Clippy, Rust’s linter, and fixed the 2,735 violations they raised. Its description counts 45 deny-level lints, while its title says 33. The changes included 1,341 // SAFETY: comments stating the invariant above each unsafe block, 840 function signature changes, 156 raw-pointer functions made explicitly unsafe or given safe parameter types, 47 uses of mem_forget replaced, and a net 6,200 lines of dead code removed.

Its commit messages show how the work was organised. Round 3 used a “per-file fixer + 2-reviewer gate”, rounds 4 to 6 a “4-shard fixer fleet”, round 14 addressed “126 PR review comments via review-round.workflow.ts”, and round 16 reached “100% clean”. There are 17 numbered rounds in all.

The same pull request added a CI job that runs Miri over the crates that do not call native code. Miri is “an Undefined Behavior detection tool for Rust” (rust-lang/miri), not formal verification, and its own documentation says it “fundamentally cannot ensure that your code is sound”. It catches undefined behaviour on the paths the tests exercise, which is useful and narrower.

What this means

For Bun users. The Rust core shipped in 1.4.0 on 20 August 2026, and the current release on 8 November is 1.4.4. GitHub’s language breakdown for the repository now shows Rust at 77.1%, C++ at 12.0% and TypeScript at 8.4%, with no Zig left.

For AI-assisted ports. Every pull request came from a branch prefixed claude/, and Bun has been owned by Anthropic since December 2025 (Bun). Claude Code itself ran on the Rust port from version 2.1.181 in June, and Sumner reports that its “Startup got 10% faster on Linux but otherwise, barely anyone noticed.” The method depended on an existing test suite strong enough to act as the judge. A codebase without one would not get the same result from the same approach.

For Zig and the wider ecosystem. Deno, the other runtime built after Node.js, is built on Rust (Deno), so Bun now shares its choice. However, Sumner’s own explanation rests on Bun’s unusual mix of garbage-collected and manually managed memory, which makes it a weak argument against Zig in general. The developer community was split on the merge: on 8 November the main pull request had 1,769 thumbs-up and 1,803 thumbs-down reactions.

A port of 1,009,257 lines reached a released version in just over three months, and the benchmarks that matter came out level with the code it replaced. What changed is the class of bug the compiler now catches before a release.

Try it with bun upgrade.

Frequently Asked Questions

Why did Bun switch from Zig to Rust?
To catch memory bugs at compile time. Jarred Sumner wrote that a large share of Bun's bugs were use-after-free, double-free and missed frees in error paths, which are compiler errors in safe Rust. He added that he does not blame Zig: Bun mixes a garbage-collected JavaScript engine with manually managed memory, which few languages are designed for.
Is Bun faster after the Rust rewrite?
Roughly level. After the follow-up work in PR #31115, the Rust bundler was 1.3% slower than Zig 1.3.14 on a 100k-file benchmark (3,443 ms against 3,398 ms wall time) while using 41% less CPU in total. The remaining gap is on the main thread.
Does Bun still use Zig after the rewrite?
No. GitHub's language breakdown for the repository on 8 November 2026 shows Rust at 77.1%, C++ at 12.0% and TypeScript at 8.4%, and no Zig.
Is the Rust rewrite available in a released version of Bun?
Yes. Bun 1.4.0, released on 20 August 2026, is the first version written in Rust. Bun 1.3.14 was the last version written in Zig.
How much of the Rust rewrite was written by AI?
All five pull requests came from branches named with a claude/ prefix, and Bun has been owned by Anthropic since December 2025. The commit messages of the lint pull request describe automated fixer and reviewer rounds. Jarred Sumner opened and merged the work, and Bun's existing test suite was the check on correctness.
bun rust rewrite bun rewrite zig to rust bun rust migration bun programming language change bun rust performance javascript runtime rust bun oven sh rust bun memory safety bun rust review zig vs rust bun

Enjoyed this article?

Get notified when I publish new articles on automation, ecommerce, and data engineering.

Get in touch

Related Articles