Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 29, 2022 03:31 pm GMT

Bun: The JavaScript runtime taking on Node.js and Deno

Written by Alex Merced

Bun is a new, blazing fast JavaScript runtime that has everyone talking. To understand why Bun is such a big deal, lets first review some important JavaScript history.

  • What is a JavaScipt engine?
  • Introducing JavaScript runtimes
  • What is Bun?
  • Taking Bun for a test drive
  • Writing and running our first Bun script
  • Writing files with Bun
  • Built in support for SQLite3
  • Using .env files with Bun

What is a JavaScript engine?

When 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:

  • V8: Created by Google for Chrome
  • SpinderMonkey: Created by Mozilla for Firefox
  • JavaScriptCore: Created by Apple for Safari

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.

Introducing JavaScript runtimes

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

What is Bun?

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.

Taking Bun for a test drive

To get started with Bun, first, well 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 youre using Windows, youll need to set up Window Subsystem for Linux to install Bun.

Once its 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.

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.

Writing files with Bun

Bun has a pretty simple API for writing to files. Lets modify our script to write to a file each time we submit a request:

let count = 1Bun.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 youll 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.

Built in support for SQLite3

Unlike other JavaScript runtimes, you dont have to install SQLite3 because its built in out of the box. Lets create a new file called db.js with the following code:

import { Database } from "bun:sqlite";// Create a new Database Fileconst db = new Database("db.sqlite3");// Create a table in the databasedb.run("CREATE TABLE IF NOT EXISTS cheeses (name VARCHAR(100));")// Insert Some Values into the tabledb.run("INSERT INTO cheeses VALUES ('gouda'), ('munster'), ('brie');")// Query the tableconst result = db.query("SELECT * FROM cheeses;").all()// Log resultsconsole.log(result)

Run the code with bun run db.js, and you should see the records that are inserted logged on the terminal.

Using .env files with Bun

Another 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, lets update our script.js with the following code:

// let count = 1Bun.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.

Conclusion

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? Well 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, its 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 its 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!

200s only Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If youre interested in ensuring requests to the backend or third party services are successful, try LogRocket.

LogRocket Sign Up

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.


Original Link: https://dev.to/logrocket/bun-the-javascript-runtime-taking-on-nodejs-and-deno-abl

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To