Skip to content

This Dot Blog

This Dot provides teams with technical leaders who bring deep knowledge of the web platform. We help teams set new standards, and deliver results predictably.

Newest First
Tags:Bun
Bun v1.0 cover image

Bun v1.0

On September 8, 2023, Bun version 1 was released as the first production-ready version of Bun, a fast, all-in-one toolkit for running, building, testing, and debugging JavaScript and TypeScript. Why a new JS runtime You may ask, we already have Node and Deno, so why would we need another javascript runtime, Well yes we had Node for a very long time, but developers face a lot of problems with it, and maybe the first problem is because it’s there for a very long time, it has been changing a lot between different versions and one of the biggest nightmares for JavaScript developers these days is upgrading the node version. Also, Node lacks support for Typescriptt. Zig programming language One of the main reasons that Bun is faster than Node, is the programming language it has been built with which is Zig. Zig is a very fast programming language, even faster than C) (here is some benchmarks), it focuses on performance and memory control. The reasons it’s faster than C is because of the LLVM optimizations it has, and also the way it handles the undefined behavior under the hood Developer Experience Bun delivers a better developer experience than Node on many levels. First, it’s almost fully compatible with Node so you can use Node packages without any issues. Also, you don’t need to worry about JS Common and ES Modules anymore, you can use both in the same file, yup you read that right, for example: ` Also, it has a built-in test framework similar to Jest or Vitest in the project so no need to install a different test framework with different bundler in the same project like Webpack or Vite ` Also, it supports JSX out-of-the-box ` Also, Bun has the fastest javascript package manager and the most efficient you can find as of the time of this post ` Bun Native APIs Bun supports the Node APIs but also they have fun and easy APIs to work with like * Bun.serve() : to create HTTP server * Bun.file() : to read and write the file system * Bun. password.hash(): to hash passwords * Bun.build(): to bundle files for the browser * Bun.FileSystemRouter(): a file system router And many more features Plugin system Bun also has an amazing plugin system that allows developers to create their own plugins and add them to the Bun ecosystem. ` Conclusion Bun is a very promising project, and it’s still in the early stages, but it has a lot of potential to be the next big thing in the JavaScript world. It’s fast, easy to use, and has a lot of amazing features. I’m very excited to see what the future holds for Bun and I’m sure it will be a very successful project....

A Look At Bun.sh: the Modern JavaScript Runtime cover image

A Look At Bun.sh: the Modern JavaScript Runtime

Bun is a modern JavaScript runtime like Node or Deno focused on speed, and performance. It is an all-in-one tool (runtime, bundler, package manager, transpiler). In this article, we will look at the excitement behind it, and dive into some features. Overview Bun is developed from scratch using the zig programming language. It uses JavaScriptCore Engine (Same with Safari browser), which is unlike Node.js and Deno, which use Chrome’s V8 engine. Bun natively implements hundreds of Node.js and Web APIs, including ~90% of Node-API functions (native modules), fs, path, buffer, and more. Plus, it supports Typescript and JSX out of the box. Getting started To install Bun on our machine, simply run the command: ` For Mac, Linux, and Windows Subsystem. Now run bun --version to verify that it is correctly installed. First Bun Script Create a javascript file called http.js and add the following: ` Now run the following: ` Then open http://localhost:3000 in your browser. You can create the same file in Typescript as http.ts and run: ` Then open http://localhost:3000 in your browser. Without modification or extra installation, we now have scripts in JavaScript and Typescript running. Features Let's dive into some of the features of bun. Packages Bun supports node packages and provides some integration with the latest React ecosystem with the create command. Bun uses node_modules.bun to house all the imported dependencies. Let’s add React to our new project. ` Bun will generate the node_module.bun file in the directory. For an existing application similar to yarn or npm to install dependencies with bun, simply run bun install in the project directory. It will use the existing package.json in combination with the lock file when present to add dependencies. Scaffolding an App To scaffold or create a new project from a template, framework (React), or blank project, use the create command. ` SQLite3 Out of the box With Bun, you don’t have to install SQLite as it’s built-in out of the box. ` Run the code with bun run db.js, and you should see the records that are inserted logged on the terminal. Environment Variables Bun automatically loads environment variables from .env files. No more require("dotenv").config() and you can simply access proccess.env without needing to install packages. Create an .env file with the following: ` Create a file http.js ` Run the code with bun run http.js, and you should see the NotReallyA_Key logged on the terminal. Conclusion Hopefully this article showed you how easy it is to get started with Bun and the features you should be excited about. The Bun approach to performance is truly a big win for the community. Bun is still young and seems to have a promising future. Will Bun replace Deno or Node? That is too early to call, as both existing runtimes have been around for a while and are actively maintained and features are being added often with constant improvements on the existing APIs. Bun does not yet have a stable release (as of this writing) and is still very early in development. What’s your opinion on bun or some exciting projects you might build or migrate with bun?...