Bootstrap your next Preact application with Bun
The History of Runtimes
However, these runtimes were built using Chromium v8 engines, which have some performance pitfalls like slow startup times and memory management, amongst others. There have been efforts to create runtimes that offer increased speed and performance, and that led to the creation of Bun.
What is Bun
- prioritizing increase in the speed of startup times, as compared to V8 which prioritizes fast execution.
- Possession of three optimizing compilers which makes it much more complex but faster, as compared to V8 which only has two compilers with less consideration on speed but on ease of use.
Before bootstrapping our application, we will install Bun. To install Bun for MacOS, Windows, or Linux, run the command below:
curl -fsSL https://bun.sh/install | bash
For macOS users, After running the command above, run this command below:
Bootstrapping our Application
Let’s look at how to set up a Preact application with Bun in this section. First, ensure that you have the Bun runtime installed. To do so, enter the command:
Typing this command will display the bun version. To get more information on the commands available in bun enter the command below:
Typing this command will display some helpful information about the commands that bun provides.
Next, we will create a Preact application using the create command displayed above. Type the command below:
On typing this command, it displays some commands to create projects of different types, like a React project, an Apollo Server project, a svelte project, and many more.
We will choose the method for developing a Preact application. To create a Preact app, type the command below:
bun create preact <name of app>
cd <name of app>
On installing the app, you can start a development server of the application with the following command:
On running the command, it will launch the preact application at localhost:3000
Preact application at localhost:3000You can also build your application for production with the following command:
Features of Bun:
1- Bun as a package manager
As described previously, Bun can be used as a package manager as it supports it natively. In order to install packages with Bun, You must have Bun installed on your system and a project already set up with Bun. The Bun install command is represented below:
To add a specific package to your project, type the command below:
bun add <package name>
To delete a specific package from your project, type the command below
bun remove <package name>
2- Bun as a fast Runtime
Firstly, we will start with creating a preact app with Bun. Navigate to your project directory and run the following command:
bun create preact <name of app>
Installation of a Preact app using Bun took less than 4 seconds, as shown in the picture below:
Next, we will start with creating a Preact app with the preact-cli method. Navigate to your project directory and run the following command:
npx preact-cli create default <name of app>
Installation of a Preact app using the preact-cli method took about a minute, as shown in the picture below:
From the test above, we can see that installing a Preact application with Bun proved to be much faster than using the preact-cli method.For more information, you can look at this GitHub repository, which contains a number of benchmark tests that you can run on Bun.
In this tutorial, we learned about the Bun runtime, how to install it, how to create a Preact app, and how to install and remove packages using Bun. We also did some tests to showcase the awesome features that Bun provides. That being said, I hope this article was able to convince you to bootstrap your next project using Bun and to take advantage of the awesome features that this new runtime provides.
No comments yet