Bun is a new, blazing fast JavaScript runtime that has everyone talking. To understand why Bun is such a big deal, let’s first review some important JavaScript history.
.env
files with BunWhen JavaScript was first created, it only ran in browsers, originally Netscape Navigator. However, developers needed software that could read JavaScript code and turn it into something that could run on the computer. This technology is known as the JavaScript engine. At the time of writing, there are three main JavaScript engines that power your favorite browsers:
Each JavaScript engine has its own minor differences in its support for the JavaScript spec, how quickly it adopts new JavaScript features, and its ease of use and performance.
Eventually, in 2009, Ryan Dahl first began to develop a tool that would allow JavaScript to run outside of the browser. When choosing an engine to build this tool around, he chose V8.
What he created was a JavaScript runtime, a tool for running JavaScript outside of the browser. It gave JavaScript access to your broader computer network and file systems for creating web servers and any type of application you can think of.
Node.js has since exploded in popularity, becoming a go-to tool in frontend and backend web development. When Node.js was created, many modern JavaScript standards didn’t exist yet, like the Fetch API, ES modules, and more.
Seeing the growth of TypeScript and the robustness of web standards, Ryan Dahl created a successor to Node.js using Rust, called Deno. Deno offered speed improvement, an embrace of web standards, and first class support of TypeScript and JSX.
In 2022, former Stripe developer Jared Sumner released Bun. Bun is a runtime developed in the Zig programming language, which also embraces web standards but aims for compatibility with Node.js APIs, so developers can easily migrate existing code.
One of the most interesting choices is that Bun uses the JavaScriptCore as its engine, unlike Node.js and Deno, which use V8. The result is a blazing fast runtime that also offers several quality of life features for JavaScript developers.
Bun also has first class integration of TypeScript and JSX. It aims to provide many of the features of transpilers, like Babel, and Bundlers like Webpack, Rollup, Snowpack, and Vite.
To get started with Bun, first, we’ll have to install it. According to the Bun documentation, installation requires only the following command:
curl https://bun.sh/install | bash
Keep in mind, this command will only work on Mac and Linux. So, if you’re using Windows, you’ll need to set up Window Subsystem for Linux to install Bun.
Once it’s done installing, make sure to read the confirmation prompt with directions for adding Bun to your PATH
. It will require you to add the following lines to your .bashrc
or .zshrc
files:
BUN_INSTALL="/home/<username>/.bun" PATH="$BUN_INSTALL/bin:$PATH"
Now, if you run bun
--version
, you should get a version number printed confirming you have installed it correctly.
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
.
Bun has a pretty simple API for writing to files. Let’s modify our script to write to a file each time we submit a request:
let count = 1 Bun.serve({ fetch(request){ Bun.write(`${count}.txt`, request.url) count += 1 return new Response("Hello World") }, }) console.log("Listening on Port 3000")
Run the code above and visit localhost:3000/cheese
, and you’ll see two new files created, 1.txt
and 2.txt
. The first argument of Bun.write
is the target of the write, like a file or stdout
, and the second argument is what to write.
Unlike other JavaScript runtimes, you don’t have to install SQLite3 because it’s built in out of the box. Let’s create a new file called db.js
with the following code:
import { Database } from "bun:sqlite"; // Create a new Database File const db = new Database("db.sqlite3"); // Create a table in the database db.run("CREATE TABLE IF NOT EXISTS cheeses (name VARCHAR(100));") // Insert Some Values into the table db.run("INSERT INTO cheeses VALUES ('gouda'), ('munster'), ('brie');") // Query the table const result = db.query("SELECT * FROM cheeses;").all() // Log results console.log(result)
Run the code with bun run db.js
, and you should see the records that are inserted logged on the terminal.
.env
files with BunAnother really nice touch is the ability to use .env
files out of the box. You can simply access them with process.env
like in Node.js without needing to install any libraries. Create an .env
file with the following command:
VARIABLE=cheddar
Now, let’s update our script.js
with the following code:
// let count = 1 Bun.serve({ fetch(request){ // Bun.write(`${count}.txt`, request.url) // count += 1 return new Response(process.env.VARIABLE) }, }) console.log("Listening on Port 3000")
Now, when we run bun run script.js
and visit localhost:3000
, we should should see the information from our .env
file being returned.
Beyond being super fast, Bun has some very nice features that make many of the more mundane tasks like writing files, managing simple databases, and using environmental variables quite easy.
Will Bun overtake Deno and challenge Node.js for its throne? We’ll have to wait and see. Bun will at least, like Deno, show off many innovations that Node.js can adopt while carving out a space of its own.
Either way, it’s a big win for JavaScript developers everywhere to have another runtime in the space. At the time of writing, Bun is still early in its development with many APIs and features not yet implemented. However, what is available so far is quite impressive, so it’s worth keeping up with.
Be sure to check out this video of my first test run of Bun and leave a comment if you have any questions. Happy coding!
There’s no doubt that frontends are getting more complex. As you add new JavaScript libraries and other dependencies to your app, you’ll need more visibility to ensure your users don’t run into unknown issues.
LogRocket is a frontend application monitoring solution that lets you replay JavaScript errors as if they happened in your own browser so you can react to bugs more effectively.
LogRocket works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app’s performance, reporting metrics like client CPU load, client memory usage, and more.
Build confidently — start monitoring for free.
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowBuild scalable admin dashboards with Filament and Laravel using Form Builder, Notifications, and Actions for clean, interactive panels.
Break down the parts of a URL and explore APIs for working with them in JavaScript, parsing them, building query strings, checking their validity, etc.
In this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.
Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]
One Reply to "Bun: The JavaScript runtime taking on Node.js and Deno"
I can’t get how a post about “blazing fast” whatever doesn’t get any performance comparison graphs. Leave that aside, the post is full of water and has nothing interesting to suggest.