Tech

Bun JavaScript runtime rewritten in Rust in six days

Developers have completed a rapid port of the Bun runtime to Rust, leveraging type lifetimes and destructors to prevent memory leaks, though the new implementation currently contains unsafe code.

Author
Owen Mercer
Markets and Finance Editor
Published
Draft
Source: Hacker News · original
Tech
No image available
A 960,000-line codebase passes 99.8% of the original test suite on Linux, aiming to resolve historical stability issues

A complete rewrite of the Bun JavaScript runtime in Rust has been completed within a six-day period, resulting in a substantial 960,000-line codebase. The new implementation successfully passes 99.8% of Bun's pre-existing test suite on Linux x64 glibc, marking a significant milestone for the project.

The primary motivation behind this rapid development effort is to address the stability issues that have historically plagued Bun, specifically memory leaks and crashes caused by manual memory management. By porting the runtime to Rust, the developers aim to leverage the language's robust type system to enforce type lifetimes and provide destructors, which should prevent these recurring problems.

While the code truly works and functions as intended, the new version currently contains unsafe code blocks. The author notes that these sections appear uglier than the original implementation, a deliberate choice intended to encourage future refactoring into safer Rust idioms. Full stability benefits are not yet realised until these unsafe sections are addressed.

The project was reportedly assisted by artificial intelligence tools, though the work involved significant human oversight rather than being generated entirely by an AI. The developer clarified that the process was not a simple command to an AI to rewrite the code without errors, highlighting the complexity of the task.

Looking ahead, the author plans to publish a detailed blog post outlining benchmarks, memory usage, and maintainability metrics for the new version. This upcoming analysis will also cover the specific process of the rewrite and the author's intention to implement their own libc before upstream Zig compiler integration occurs.

The rewrite is currently verified on Linux x64 glibc, with results on other platforms pending. The author anticipates that the new Rust port will compile faster than using the upstream Zig compiler, particularly when utilising a custom Zig compiler developed by the team.

Continue reading

More from Tech

Read next: Apple opens developer access to iOS, iPadOS and macOS 27 betas
Read next: Apple confirms macOS 27 Golden Gate requires Apple Silicon, ending Intel support
Read next: Apple unveils watchOS 27 with Siri AI integration and hardware restrictions