Bun processes about three times more HTTP requests per second than Node.js when rendering React server-side. WebSocket, ReadableStream, and fetch are built in. Bun is also a transpiler so TypeScript and JSX work out of the box. Because Bun implements Node.js’ module resolution algorithm, npm packages also work.
Beating a Slow Iteration Cycle
It was the speed that first brought on the need for something new that led to the creation of Bun. At that time, Sumner was developing a game in Next.js but was frustrated with the speed of the iteration cycle. The first solution was using the esbuild builder with Next.js but that didn’t bring the result he was looking for even after getting the technologies to work with plugins. Sumner’s next idea was to build a bundler and a transpiler but in the end, he built a runtime that includes a bundler and transpiler.
After taking a look at a number of languages to build Bun, Sumner ultimately landed on the relatively little-known Zig. Zig’s “comptime” metaprogramming approach helped seal the deal. Comptime is metaprogramming based on compile-time code execution and lazy evaluation.
In Zig, the programmer can label a variable at compile time guaranteeing to the compiler that every load and store of the variable is performed at compile time. Essentially, comptime can run arbitrary code at compile time. “I just tried it and realized comptime would be an incredibly powerful tool for writing fast code,” Sumner explained.
Before Bun, Sumner never wrote a line of code in Zig. He credits the helpfulness of the discord server as one of the reasons why he was able to write Bun completely in Zig.
Comptime is used in the lexical analyzer, called the “lexer,” one of the slowest parts of parsing for Bun. Its job is to iterate through all of the text to determine if the current token is a valid identifier or a keyword. Because of comptime, Bun uses a bit-set that’s generated at compile time. This process gives a 2% performance boost to Bun.
For technical challenges, of which there were many, Sumner specifically states, “One of the things that working on Bun really taught me a lot is how to read error messages.” He’s referring to the phrasing in Zig’s error messages looking more similar to those in compilers. For example, the word token, which is mostly seen in lexing or parsing, was actually very common in the error messages he was receiving.
Sumner plans to have at least two permission models in the future with one being binary dead code elimination. This model will turn features on and off so if a feature is not used in an application, Bun will not have the code. Since there is a performance hit each time a permission is hit, there will be less performance hits depending on how many features are turned off.
Shaking the Bugs Out
Developers and Sumner alike are very excited about Bun and though it’s still new, there are very high hopes for it. Sumner said, “I’ve been coming from the perspective of ‘it needs to work as much as possible with the existing ecosystem because people shouldn’t have to rewrite their code’ but maybe I should start thinking more about what if we make/what new things are possible because the APIs bun enables.”