Menu

What is Bun, the new JavaScript runtime on the block

What is Bun, the new JavaScript runtime on the block

Bun is the ultra fast JavaScript runtime that has taken the internet by storm and that is set to dominate the JavaScript development market in the coming years. At least that's what people are saying online. But whether there's any truth to that, is another story.

In my experience (20+ years), rarely does anything that is fresh and new dominate an entire segment worth billions of dollars. It'll be a tough battle to say the least for Bun.

But Bun is interesting, because it's good. Surprisingly good. And it's kept my interest long enough for me to write an entire article about it. Let's dig into what Bun is and how it stands against the competition.

What is a JavaScript engine?

Before we get into Bun directly, let's get into what a JS engine actually is and does.

For as long as developers can remember, JavaScript was mainly confined to running inside of a web browser.

And to do so, it needs some kind of engine to parse the JavaScript and to turn it into a functional application that users can interact with and see.

Welcome to JavaScript engines.

There is a good chance that you have at least heard of 1 popular JavaScript engine, that being the V8 engine created by Google and intended for use on Chrome. It is also the engine used by Node.js currently.

It's popular and for good reason. It's well maintained and it is backed by Google. Two big reasons. But there are a few competitors that it needs to contend with.

SpiderMonkey is the JavaScript engine that Mozilla uses for its FireFox browsers, which also implements WebAssembly as well.

And JavaScriptCore is the engine created by Apple for it's Safari browser. And JavaScriptCore is fast. Typically faster than the other 2.

You can see a pattern here. Essentially, every major browser currently on the market had to create its own JavaScript engine. But in some cases, they might use an existing engine such as Opera, which uses V8 internally.

What is a runtime?

And a runtime, is essentially where a program is executed on your machine, using one of these engines.

Node.js and Deno are currently the dominant forces in that area, both using the V8 engine from Google and both created (and co-created) by Ryan Dahl.

What is Bun?

Bun is a JavaScript runtime, much like Node.js and Deno, that is built from the ground up to focus on speed and overall performance and if the benchmarks are true, then it pretty dominates over its competitors. And it comes out of the box with plenty of features (down below) that can definitely replace your current JavaScript engine implementations.

Bun implements hundreds of Node.js and Web API's and it supports fs, path and Buffer as well. You can currently install Bun on MacOS x64, Linux x64 or Windows Subsystem for Linux (WSL).

The official GitHub page for Bun can be found here.

But let's get into the features that really make Bun standout above the rest, because there's quite a few.

Features

Bun currently supports Web API's, such as fetchWebSocket and ReadableStream. And from the sounds of it, it looks like it will be able to support any new API's added in the future.

Bun also transpiles every file, meaning that TypeScript and JSX work out of the box without requiring any extra dependencies.

Bun also automatically loads environment variables from any .env files with needing any extra requires or imports. Meaning overall less overhead when it comes to setting up a new project.

And currently, Bun implements around 90% of Node API functions, again, meaning that incorporating the new runtime into your projects will be a near seamless process.

And lastly, though not least, Bun also comes packaged with a SQLite3 client, bun:sqlite. And based on the queries per second that it can perform, it is several times faster than some of its competitors, such as better-sqlite3 or deno.

So in a sense, Bun is a feature-rich, built from scratch, and high performing JavaScript runtime with vital components built right into the engine from the start.

Why is it fast?

You might be wondering, if bun is so lighting quick, then why doesn't Node.js catch up in performance as well using the same techniques?

This feat of performance is due to a few factors. Starting with the fact that Bun uses the JavaScriptCore engine and not the V8 engine, which Node makes use of. And JavaScriptCore tends to perform faster than its alternatives, though that might be dependent on the environment that it is running in.

And if you haven't heard of JavaScriptCore, it is essentially a framework that provides a JS engine for implementations of WebKit, and it performs very well.

And secondly, Bun is written using the Zip programming language, which is a low-level language with manual memory management, meaning that the programming team has much more control over memory usage.

And lastly, Bun is essentially built from scratch using these high performing frameworks and languages, meaning that every part of its implementation gets a performance benefit.

Did I mention that it's fast?

How to get started

The best way to get started with Bun, is to install it onto your local machine and to take it for a test run yourself. As mentioned, Bun is currently supported on macOS x64, Linux x64 and Windows Subsystem for Linux (WSL).

And if you aren't familiar with WSL, that probably means that you're machine isn't setup for it just yet. So head on over to the official documentation to install it beforehand.

You can run the following command in order to install it:

curl https://bun.sh/install | bash

Once bun is done installing, you'll want to verify that it is running correctly, and you can do that by running the following command to check the current version number:

bun --version

You can then write and run a simple "Hello World" application to test and ensure that everything is running accordingly.


// http.js
export default {
  port: 3000,
  fetch(request) {
    return new Response("Hello World");
  },
};

And running your app is as simple as:

bun run http.js

You can then check out your localhost URL to see your application in all of its glory. Is it going to be blazingly fast? Probably, as there isn't much going on there.

Setting up a real performance test might take some time however, because Node.js and Deno are still very fast engines. And if you are just performing single threaded, asynchronous operations with low complexity, then the different in milliseconds might seem trivial.

Will it replace Node.js?

Personally, I love what I see being done here with Bun. The performance first approach will become vital in the coming years as applications get more and more complex and as data sets get larger and larger.

But it still has a long way to go before it can play with the big players. For one, most vendors currently do not support Bun as an official runtime.

I personally host websites on Vercel, which supports Node.js very well. But as of right now, there is no support for Bun. And that goes for most major JavaScript hosting platforms.

But you always have the option of setting up your own custom server with whatever runtime you wish. And for now, that might be enough to start to get some traction.

More notable though, is that Bun is still very young and it isn't done implementing all current Node.js API's. And once it has done so, we still have to see if it performs equally as well. Because what Node.js and Deno have going for them is that they have been around for a while now, so plenty of bugs have come to light and have been fixed.

But in world of JavaScript engines, it is definitely a step in the right direction to have more competition as the current selection has been limited for a number of years now.

If Bun isn't going to win the engine game in the coming years, I'm still rooting it for its attempt.

Walter G. author of blog post
Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.

Get the latest programming news directly in your inbox!

Have a question on this article?

You can leave me a question on this particular article (or any other really).

Ask a question

Community Comments

No comments posted yet
Ad Unit

Current Poll

Total Votes:
Q:
Submit

Add a comment