Delving Developer

Bun - The new Javascript runtime trying to take on Node & Deno

Eddie Cunningham
Eddie Cunningham
3 min readJavascript
Cover Image for Bun - The new Javascript runtime trying to take on Node & Deno

If you've had your ear to the ground over the last few weeks, you'll probably have heard lots of chatter about Bun, a new Javascript runtime. Or at the very least, you'll have seen its cute mascot. There have been reports all over the internet on how fast Bun is, claiming it to be the all-in-one tool to take down Node.

What is Bun?link

Bun is a Javascript runtime that uses the JavascriptCore engine. For context, when we talk about a runtime we mean where your code is executed. For example, if you're using Node you're using Chrome's V8 JavaScript engine. Bun is ambitiously aiming to replace that, and its creator Jarred Sumner](https://github.com/Jarred-Sumner) has some lofty goals. The core focus is:

  • Start fast
  • Be as performant as possible
  • Offer a complete package

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

Jarred Sumner

And he means it. Bun can reportedly serve around three times as many HTTP requests per second, over double the number of SQLite queries, and hash nearly twice as fast as either Node or Deno. That's some impressive stats, and if true will have a huge impact on the web. Whether that's via increased developer productivity or enhanced site performance, Bun is promising.

But that's not all that Jarred is trying to achieve. Let's dive into the "complete package" that Bun offers.

Package manager

Because Bun implements Node.js’ module resolution algorithm, installing npm packages works out of the box. The setup is super simple, and as you'd expect, is a similar syntax to yarn/npm:

Install packages from package.json:

bun install

Add or remove packages from package.json:

# add
bun add react

# remove
bun remove react

Transpiler

Bun has a great out-of-the-box offering. The great news for Typescript fans is that you can drop in your .ts or .tsx files and it just works. It'll pick up your tsconfig.json as usual, and even transpile any .ts and .tsx files found in your node_modules. You've probably noticed the mention of `.tsx``. But what about JSX? Not a problem, that'll get transpiled too!

If you want to access the transpiler directly, they also offer a delicious API. Pass in your configuration (loader, target platform, tsconfig etc.) and instantiate your class. Pass your string to the transform or transformAsync methods, and Bun will take care of the rest.

Bundler

Bun's main focus at the moment is on the Javascript runtime itself. That hasn't stopped them from offering a bundler, however. It's important to note that at the moment, Bun cannot minify or tree-shake. This will probably rule it out for anyone looking to build for browsers. They do state that in the future, they wish "to replace Node.js, Webpack, Babel, yarn, and PostCSS".

For now, you can simply run bun bun ./path-to-your-file.js, and a node_modules.bun will be generated that contains all dependencies.

Can I use Bun in production?link

At the time of writing this article, Bun is currently at version 0.1.8. It's far from complete and is still missing some of Node's API functions (although they claim they're at ~90% parity!). It's also worth noting that it still doesn't support minification or tree-shaking, and therefore for frontend at least, you'd still need to introduce a build tool. However, their long-term goal is to replace all of these tools - Webpack, Babel, PostCSS and so on!

Check out Bun's Github for a list of features that are still yet to be implemented.

Conclusionlink

Bun has exploded out of the gate with some impressive stats. However, it still has a little way to go before we can consider it production-ready. If Bun's promises are to be believed, our development experience is about to be majorly improved soon!

If you're as excited as we are about the future of Bun, you can check out their roadmap on Github!