Bun: The JavaScript runtime that competes with Node.js and Deno

What exactly is a JavaScript engine?

JavaScript was designed to run only in browsers, initially Netscape Navigator. However, developers need tools that could read JavaScript code and convert it into computer-readable code. The JavaScript engine is the name given to this technology. There are three major JavaScript engines that power your preferred browsers at the time of writing:

  • V8: A Google Chrome extension.
  • Mozilla created SpinderMonkey for Firefox.
  • Apple developed JavaScriptCore for Safari.

In 2009, Ryan Dahl began working on a technology that would allow JavaScript to execute outside of the browser. He picked a V8 engine as the foundation for this tool.

He developed a JavaScript runtime, which is a mechanism for executing JavaScript outside of the browser. It granted JavaScript access to your whole computer network and file systems, allowing you to create web servers and any other form of application you can dream of.

Since then, Node.js has grown in popularity, becoming a must-have technology for frontend and backend web development. Many contemporary JavaScript standards, such as the Fetch API, ES modules, and others, did not exist when Node.js was built.

Seeing the emergence of TypeScript and the resilience of web standards, Ryan Dahl designed Deno, a Node.js replacement written in Rust. Deno provided faster performance, support for web standards, and first-rate TypeScript and JSX compatibility.

What exactly is Bun?

Former Stripe engineer Jared Sumner launched Bun in 2022. Bun is a runtime written in the Zig programming language that supports web standards while also aiming for interoperability with Node.js APIs, allowing developers to quickly move existing code.

One of the most intriguing choices is that Bun, unlike Node.js and Deno, uses the JavaScriptCore engine. As a consequence, the runtime is lightning fast while simultaneously providing various quality of life features for JavaScript writers.

Putting Bun through his paces

To get started with Bun, we'll need to install it first. The Bun documentation states that installation takes simply the following command:

bash curl https://bun.sh/install

Keep in mind that this command is only available for Mac and Linux. If you're using Windows, you'll need to install Window Subsystem for Linux before you can install Bun.

When it's finished installing, see the confirmation popup for instructions on adding Bun to your PATH. It will necessitate adding the following lines to your .bashrc or .zshrc files:

BUN INSTALL="/home/<username>/.bun"

PATH="$BUN INSTALL/bin:$PATH"

If you run bun—version again, you should get a version number that confirms you installed it successfully.

Writing and running our first Bun script

Create a file called script.js and add the following code inside it:

Bun.serve({
    fetch(request){
        return new Response("Hello World")
    }
})
console.log("Listening on Port 3000")

Bun.serve initiates the server and takes an object with the server configurations. On each request, the request object is passed to a function stored as the fetch property on the configuration object.

We can run Bun.serve by using the command bun run script.js and then going to localhost:3000 to see the response to our request. If we wanted to change which port it will serve on, we can add a port property to the object passed to Bun.serve.

Using Bun to create files

Bun offers a straightforward API for writing to files. Let's change our script such that it writes to a file every time we make a request:

let count = 1 \sBun.serve({ \s fetch(request){ \s Bun.write(`${count}.txt`, request.url) \s count += 1 \s return new Response("Hello World") \s }, \s})
console.log("Listening on Port 3000") ("Listening on Port 3000")

When you run the code above and navigate to localhost:3000/cheese, you'll notice two new files, 1.txt and 2.txt. Bun.write takes two arguments: the target of the write, such as a file or stdout, and what to write.

Conclusion

Bun, besides from being extremely quick, has several really good features that make many of the more boring chores, like as writing files, maintaining basic databases, and utilizing environmental variables, very simple.

Will Bun dethrone Deno and challenge Node.js for the throne? We'll have to see what happens. Bun, like Deno, will demonstrate several ideas that Node.js may take while carving out its own territory.