Finally, we’ll perform some performance tests to verify that Bun is as quick as it claims to be on its website.
What is Bun runtime
It asserts to be quicker and more feature-rich than Node.js and Deno. Bun was designed by Jarred Sumner and made available to the general public in 2021. Since then, it has quickly racked up more than 30k GitHub stars.
Features of Bun
Bun’s task runner is comparable to Node.js, however, it moves much more quickly. To benefit from its speed, you can use Bun’s task manager in a Node project. A necessary tool is a task runner, which both Node/npm and Deno come with by default. Additionally, whereas npm can only do this for a select few script names, Bun’s task manager can launch nearly any script without the requirement for the run subcommand.
Bun is designed to be easy to deploy and manage, with support for containerization and orchestration using Kubernetes. This makes it a good choice for building modern, cloud-native applications that need to be scalable, resilient, and easy to manage.
Using Node.js with Bun without modification
For interacting with SQLite databases, Bun comes with a quick and effective built-in SQLite3 client. The client is perfect for contexts with limited resources because it is speed-optimized and has a small memory footprint. It also includes an easy-to-use, straightforward API.
Why is Bun faster
Lightweight: Bun is a lightweight runtime, which means it has a smaller codebase and needs fewer resources, making it perform better than other Runtimes in terms of speed and memory utilization.
Compactness: Bun contains a native transpiler that allows you to write Typescript code right out of the box and a native bundler to take the place of tools like Webpack. Like Jest, Bun features a test runner. You don’t need to install dozens into each project; environment variables load automatically.
Low-level code: Zig, a low-level programming language that is more recent than C or Rust, is used to create the Bun runtime.
Installation and setup of Bun runtime
You can visit the website and see the official document on how to install Bun, although below is the command:
This is supported on macOS, Linux, and Windows.
Bun, as a package manager
In fact, according to Bun’s official GitHub page, Bun can install packages 20 times to 100 times faster than npm install, and on macOS, you can record up to 4 times to 80 times faster than npm install.
You need Bun installed on your system and a project set up to use Bun install. The Bun install command has the following syntax:
<code>bun install</code>Code language: HTML, XML (xml)
To add packages, use the following:
<code>bun add <package_name></code>Code language: HTML, XML (xml)
To remove packages, use the following:
<code>bun remove <package_name></code>Code language: HTML, XML (xml)
Combining Bun and React
Let’s set up React application with Bun.
<code>bun --version</code>Code language: HTML, XML (xml)
The command above verifies that you have Bun runtime installed, your current version of Bun will be displayed on your terminal. Visit their website to see the official documentation for the installation guides on your system.
<code>bun create</code>Code language: HTML, XML (xml)
The command above previews tools to create.
From the list above, you can see React command, now let’s run it:
<code>bun create react [app-name]</code>Code language: HTML, XML (xml)
Now run a development build so your application can start running by using this command:
<code> bun dev</code>Code language: HTML, XML (xml)
Bun runtime speed test
To determine which of these runtimes executes the code the quickest, let’s conduct a quick speed test. Our software consists of a single loop that we start at program startup and stop when the loop completes its iteration.
Below is our code block for the program:
- Make a file and give it any name you like; for example, bun-runtime.js.
- In the file, paste the code above in the file.
- Navigate to your terminal, then go to the folder containing the file.
- Run the program below:
bun [file name]
See the outcomes below:
Bun completed its loop at 22.39ms
For Node.js run the command below:
node [file name]
Node completed its loop at 163.225ms
Bun speed test with react
Now that we have installed our Bun and its unique features, let’s test the speed of creating a React project using Bun and using create-react-app.
Go to your terminal and run the command below:
bun create react [your-app-name]
Creating the React app using Bun didn’t take up to 10 seconds.
For our create-react-app open the terminal and run the command below:
npx create-react-app [app-name]
It took React app over 60 seconds to finish installing all dependencies for a new React application.
In this article, we knew about Bun runtime, its installation method, features, and how to use it, Bun is a versatile and powerful tool that benefits web developers looking to build fast, efficient and scalable applications. You can also visit the GitHub page containing lots of benchmark tests you can take out on Bun.
Here are more posts about web and app performance for you:
Why is Web Performance More Important Than Ever?
Boosting Performance with Lazy Loading