Bun: The Next Big Thing in Javascript

Kinanee Samson - Sep 12 '23 - - Dev Community

Bun is a Javascript runtime that claims to be 4x faster than NodeJS. Bun is an all-in-one Javascript runtime & toolkit. Bun ships as a single executable that can be installed on your computer. Bun is written in Zig, a low-level general-purpose programming language. Zig is an imperative, general-purpose, statically typed, compiled system programming language. Zig is supposed to be a replacement for C and C++. Zig is designed to be smaller and simpler to program in while also offering modern features, so you'd expect applications built with Zig to be quite fast, a feature that spills into Bun.

Bun also ships as a package manager that can be installed with the npm install bun command, which Bun also claims will be the last npm command you ever run, I'll keep an eye on this because the bum package manager claims to be 33X faster than npm when installing packages and all existing npm packages can be installed with the bun package manager.

Bun is also a test runner and it is a direct replacement for existing test runners because it claims to be 32X faster than Jest+Babel and 8X than Vitest. These are crazy numbers. A stable 1.0 version of Bun was recently released in September 2023.

Bun is designed with three main goals in mind;

  • Speed
  • Simplicity
  • Providing a full development ecosystem

In today's post, I'm going to go over 6 reasons why I think Bun is the next big thing since Typescript. Here are the main talking points;

  • Full compatibility with NodeJS
  • Out-of-the-box support for Typescript
  • Consistent module imports
  • Default Watch Mode
  • Bun Internal APIs
  • Compact Development Experience
  • Why should you care?

Full compatibility with NodeJS

Bun is intended to be a direct replacement for NodeJS, it offers full support for all of the existing NodeJS APIs and you will feel just at home like you're working with NodeJS. This is important because most developers would rather just stick to what works especially if they have to start adapting to using newer APIs. The full compatibility with NodeJS will significantly reduce the learning or adoption curve.

Out-of-the-box support for Typescript

Bun has first-class support for Typescript and it will directly execute your Typescript modules without any further compilation step. This can significantly reduce your overall build step because you are not worried about converting your Typescript to valid Javascript, which has already been taken care of.

Consistent module imports

Bun allows you to do what I call a "consistent module import" This catchy phrase just means that Bun reduces all your worries about moduleResolution. You can use import {} from 'file' or require('module') in any of your modules and you can even mix them in the same file and everything will work just fine, eliminating the need to configure and use a consistent module Resolution throughout your codebase.

Default Watch Mode

Bun has a default watch mode in which you can run your apps. This will allow you to preview live changes in your applications as they happen without having to configure them or install a package for that. This is in stark contrast to NodeJS where you have to install the nodemon package if you want to develop your application in watch mode. This is another bonus to your overall workflow because you don't need to set up nodemon for any of your future projects on Bun.

Internal APIs

Buns also has its internal APIs that are different from the core NodeJS APIs. Although they offer the same functionalities as the NodeJS API they're more elegant and simpler to use when compared to Nodejs core APIs. There is the Bun.serve({}) which allows you to quickly spin up a server. Compare this to how many lines of code it would take to spin up a server in NodeJS. There are also APIs for reading and writing to files that are more elegant than what we currently have in NodeJS.

Compact Development Experience

The overall goal of Bun is to provide developers with a complete toolbox for developing applications from a single platform. Most of the existing steps in application setup and development are already taken care of by Bun. Allowing you to focus on developing your application as fast as possible as opposed to trying to set up your development environment first. Gone are the days of npm I demon, gone are also the days of npm i -D @types/package. This is another reason why Bun is so easy to use.

There are other awesome features of Bun that we had to skip in this post for time's sake nonetheless here is a quick breakdown;

  • Bun implements most of the Existing Web APIs directly into the run time, things like fetch, alert, confirm, etc.
  • Bun supports .jsx and .tsx files out of the box.
  • Bun has the fastest package manager with an install speed of less than half a second.

Why should you care?

Does this mean that we should all uninstall our NodeJS and start installing Bun, this is a highly unlikely situation because people adopt things slowly, so it's going to take some time before we see a large-scale adoption of Bun if at all it happens. Besides there are also some drawbacks to getting started with Bun.

  • It is only available for installation on Linux and Mac OS, there is no Windows-compatible version of Bun, The only way to get around this is to install the WSL version.

I found this to be very annoying as I am currently using a Windows PC and I don't want to set up WSL on it. But anyway let me know what your thoughts are on Bun, do you think that It will replace NodeJS and npm? How fast do you think that could happen if at all it will happen? What would you like me to write about Next Still on the Bun? I will see you in the next post.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .