The Code Report

It is September 8th 2023

JavaScript and Bun

A New Era for JavaScript Development

Yesterday was a huge day for soy devs. Mojo became generally available, a super set of python that’s up to 35,000 times faster. But JavaScript also got way faster thanks to a new Swiss army knife called Bun. That means it’s time for some new stickers on my MacBook Pro because JavaScript developers have grown tired of reinventing new frameworks. So, now we’re building an entirely new JavaScript runtime. In the beginning, we had Node.js, then rearranged the letters to make Dino. Now, we’ve got Bun fresh out of the oven, which just hit 1.0 this morning. You should take notice because it can run your JavaScript code faster than any other tool known to man. But not just that, it also acts as a bundler, like Webpack or Veet, a test runner like Jest or Karma, and a package manager like npm or yarn.

DevGPT

In addition, it’s almost fully compatible with existing Node.js APIs but has its own set of highly optimized APIs that actually make it fun to build high-performance server-side applications with JavaScript. But before we get into it, I want to show you another awesome new tool from our sponsor, DevGPT. By now, you’ve heard of tools like ChatGPT and Co-Pilot, but DevGPT takes AI code generation to the next level by integrating directly with your Git repo, giving the AI the full context of your tech stack and project. After writing a prompt to fix a bug or create a feature, it’ll ask follow-up questions to write highly targeted and useful code. Once the code is generated, you can then sync those changes to your local project instantly. Then you could prompt it to generate some unit tests to validate that code. It’s like having an automated engineer by your side who can take care of your GitHub issues and Jira tickets instantly without complaining. You can try it out for free yourself right now with the link below. But now, let’s get back to Bun.

Performance

First of all, how do they make this thing so fast? One major difference compared to Node and Dino, which uses the V8 engine from Chrome, Bun uses JavaScript Core from Safari, which is known to be optimized for faster startup times. In addition, Bun is implemented in the Zig programming language. They’ve put a focus on profiling and performance bottlenecks and implementing APIs in native code. The end result is a “Hello, World” with TypeScript that’s five times faster than esbuild. Keep in mind, though, benchmarks can always be cherry-picked and rarely reflect real-world performance. It’s fun to go fast, but because I only have 12 monthly active users on my website, it doesn’t make a huge difference for me. What I care about is the developer experience. Can Bun make my life easier?

Developer Experience

Well, first up, it’s got awesome TypeScript support out of the box. You can immediately start writing TypeScript code without any configuration or headaches. It also supports JSX and hot reloading, which means you don’t need any weird dependencies like nodemon and concurrently to recompile your code when things change. One of the absolute worst things about Node has been the transition from CommonJS to ES modules. What’s cool about Bun is that you can just use import and require in the same file, and everything just magically works. It’s compatible with existing Node.js packages, which means you don’t have to reinvent the wheel for every little thing you want to implement. Dino also added npm support recently, but what I really like about Bun is its native APIs. For example, to read and write to the file system, we can access the global Bun runtime and make a reference to a file, then lazily read it or parse its JSON.

Frameworks and Simplifications

That may not seem like a big deal, but in Node.js, there are three different ways to do the same thing: read file with callbacks, read file with promises, or read file sync. You can actually use all of those in Bun, but not only are the native APIs faster, they’re easier to use. In addition, it’s extremely easy to set up an HTTP server in Bun by simply calling the `serve` method. You can then use familiar browser primitives like Fetch and Response to build your API. But there’s also frameworks that work with Bun like Elysia that claims to be 18 times faster than Express.js and Node. Bun has tons of other simplifications as well, like it makes WebSockets nice and simple out of the box without any extra dependencies, and you can use environment variables without things like Dotenv.

Bundling and Package Management

But as the name implies, Bun is also a bundler. The `bun build` command will take all your source files and then combine them into a single output that can be used in a browser. And based on this benchmark, it’s pretty amazing how much faster things have gotten just since Webpack 5 a couple of years ago. Another really cool thing is that Bun has a SQLite database built directly into the runtime. It’s incredibly convenient to have a relational database without the need to install and configure a bunch of drivers. Another thing we need to talk about is the Bun package manager that’s up to 25 times faster than npm. It’s actually a standalone tool that can be used in any Node.js project today. If you want to make things go faster right now, replace `npx` with a command like `bunks –cal say “hello, world”`, which allows you to save a few precious milliseconds when saying hi to your mom, which is important because you never know how much time you have left to do that.

This has been The Code Report. Thanks for watching, and I will see you in the next one.


LEAVE A REPLY

Please enter your comment!
Please enter your name here