4

Meet Bun: A JavaScript Runtime for the Whole Dev Lifecycle

 2 years ago
source link: https://thenewstack.io/meet-bun-a-javascript-runtime-for-the-whole-dev-lifecycle/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Meet Bun: A JavaScript Runtime for the Whole Dev Lifecycle

18 Jul 2022 8:28am, by Jessica Wachtel
9718f867-bun.png

Designed as a drop-in replacement for running JavaScript and Typescript applications, Bun, a new JavaScript runtime built with a JavaScriptCore engine, was designed to be an all-in-one tool for bundling, transpiling, and running code at lightning speed.

Built by Jarred Sumner, a former frontend engineer at Stripe, Bun aims to “run most of the world’s JavaScript outside of browsers, bringing performance and complexity enhancements to your future infrastructure, as well as developer productivity through better, simpler tooling,” Sumner said in a YouTube interview, conducted by Ping Labs CEO Theo Browne.

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.

“I’ve been frustrated by how slow everything is in JavaScript and I think, rather, I know, that JavaScript can get a lot faster,” Sumner said.

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.

In the interview, Sumner explained that he knew JavaScript could be faster, because of an experience he had writing native code in Objective-C. The speed of Apple’s NSLog, which is the Objective-C equivalent of console.log in JavaScript, showed him how quickly it was possible to write to the terminal. It was a window of insight into a possible workflow that was slowing JavaScript applications.

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.

Right now, Sumner recommends Railway or fly.io as the best deployment options. There will be a built-in option but it’s too early for any additional details. Express, Angular, and Jest’s testing APIs are specific technologies Sumner is working on adding proper support for but on JavaScript frameworks in general he says, “I wouldn’t say I’m competing with any framework because I want all of these frameworks to use Bun.”

Shaking the Bugs Out

And last but certainly not least, testing. Bun is new and it has bugs. Sumner is aware of this and credits the lack of widespread testing as one of the reasons why it’s so buggy. Of this he says, “bun doesn’t have enough test coverage and that’s why it’s so buggy.” Bun has a JavaScript test runner and the cli command is bun wiptest.

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.”


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK