Discovering Bun, the future replacement for NodeJS?

— Article in partnership with talent.io —

Everyone talks about it but few have tested it yet. I suggest that today we set off to discover Bun.sh.

Indeed, when you work as a developer, it’s important to keep up to date with the evolution of technologies like Bun or NodeJS. In the same way, when you seek to evolve in your work, it is good to know the salary scale in France. That’s good since talent.io has published a study of tech salaries in 2022 so that you can determine whether your salary is correct or undervalued.

But back on topic. Presented as the new NodeJS, Bun is a javascript platform which unlike NodeJS does not use the famous Chromium V8 engine but JavascriptCore. It is a framework derived from KJS (KDE’s JS engine) used for Webkit implementations within macOS.

The philosophy behind Bun.sh is to provide a javascript runtime that starts quickly and has excellent performance levels. Much more than NodeJS or even Deno. This is not surprising because in addition to the use of JavascriptCore, Bun is coded in ziga low-level programming language that offers good performance.

Moreover, it is a tool that wants to be complete since it is both a package manager (an npm client faster than the original or than yarn), a bundler and a transpiler which includes both JSX and Typescript. It implements hundreds of NodeJS APIs and Web APIs as well as 90% of native NodeJS functions like Node-API, fs, path, buffer…etc.

Even if Bun brings a huge gain in terms of pure performance but also a time saving thanks to its all-in-one aspect, it is still very young since its beta was released in July 2022.

I still offer you a small tutorial to install and test Bun.sh.

How to install Bun.sh

To deploy Bun.sh to your machine, open a terminal on macOS, Linux, or Windows WSL and enter the following command:

curl https://bun.sh/install | bash

Logically, the install script added the following line to your $PATH in the ~/.zshrc file:

~/.bun/bin

In your zsh terminal (exec /bin/zsh), then run the following command to check that bun launches correctly:

bun --version

By running the command:

bun --help

You will also see the commands available within Bun such as “run” to launch JS, “dev” to start a development server, “create” to create a Bun project from a template, etc.

Your first Bun script

As a test, we will first create an http server that will broadcast our hello world like this in a .js file:

export default {
    port: 3000,
    fetch(request) {
        return new Response("Hello World from Bun!");
    },
};

Then we launch this .js file with the “bun run” command. This is the same command that allows running JavaScript and TypeScript files as well as package.json scripts. By replacing “npm run” with “bun run”, your launches will be 30 times faster.

FYI, I named my script: buntest.js.

bun run buntest.js

Then go to the localhost:3000 address with a browser and you will see the Hello World displayed:

Bun as package manager

We will now focus on the package manager aspect of Bun, which replaces yarn or npm install and allows you to install packages 20 times faster. For example, if I want to install a lib and its dependencies, I just have to run the command:

bun install nom_de_la_lib

Exactly how one would do it with the npm command.

Deploy an app with Bun

If in your terminal, you type the following command:

bun c

You will see a list of project templates you can create with Bun. For example, to create a react project, you just need to enter the command:

bun create react ./react-app

Once the template is deployed, do a:

cd react-app

Then start the dev server:

bun dev

Then by going to the url: http://localhost:3000, you will see the home page of your new React project.

Conclusion

So much for setting up and using Bun. On the operation, it is very similar to NodeJS but it is especially very fast. However, I felt that it was still in beta because I still encountered some bugs (also documented on their github) and even a segfault. So Bun is still a long way from sweeping NodeJS but once these teething issues are resolved it will be a great javascript runtime.

Bun is therefore not to be used in production at the moment because there are still some big problems, not to mention the absence of minification of the code in the bundler. So it’s still not optimal. But it’s a project to keep an eye on because it’s progressing quickly and looks very promising.

Now if you think that technically, you are stagnating in your current job, you may want to change jobs to continue to evolve in technologies, or to obtain a better salary. It’s a process that can be quite complicated, sometimes stressful, as we want to find a job that suits us. We may wish to have a job closer to home or even complete telecommuting, a better salary, a better atmosphere at work, or even all of this at the same time (but it is not necessarily easy to find).

Luckily, talent.io is here to help. Simply create an account on the talent.io platform in a few clicks to then receive offers from companies that match your specific criteria and who have an entry-level salary. talent.io is really the easiest way to find your next tech job, elsewhere, registrants find their jobs in an average of 20 days.

5/5 - (1 vote)
SAKHRI Mohamed
SAKHRI Mohamed

The blog of a computer enthusiast who shares news, tutorials, tips, online tools and software for Windows, macOS, Linux, Web designer and Video games.

Articles: 3764

Leave a Reply

Your email address will not be published.