What is BUN
Tags: bunjs, webdev, javascript
Published: 2023-09-26 04:21:26

> '' This article is published using [Lamento](https://www.lamento.in/)🍋

## The All Rounder:

Bun is a all in one toolkit for JavaScript developer helps us to do the same task (running, building, testing, and debugging) but faster 

> What's the point of having hi-end gaming PC when the player isn't good at the game he is just bad but faster 

**What makes BUN special?** it's fast 

**What makes BUN fast?** BUN is written in ZIG and uses JavaScriptCore engine which powers WebKit browsers such as Safari while Node and Deno uses V8. . The team has spent a ton of time optimizing it

## Bun has 4 main trump cards: 🎴

### JavaScript runtime" 🏃

**Node.js compatibility & BUN APIs**

A node and Deno alternative but has built-in support for Node APIs which makes existing node.js project and npm package works fine with bun. Although most of the Node API works with BUN but its not completely compatible with Node do cheack this Node.js compatibility to know more about supported APIs. BUN has its own standard-library APIs are designed to be fast and easy-to-use.

**How BUN handles ESM issue** 

Bun supports both CommonJS and ES modules there is no need to including "type": "module" in package.json and it can even use import and require(), in the same file

`import lodash from "lodash";
const express = require('express')`

**Database:**

Bun has built-in support for SQLite. Which has written in written in native code to be faster.

**Framework:**

BUN can be paired with Server frameworks (Express, Koa, and Hono), full-stack frameworks (Next.js, Remix, Nuxt, Astro, SvelteKit, Nest, SolidStart, and Vite)

### Package manager"💼

BUN isn't the first one who has tried to speed up NPM, there are yarn and pnpn who have their own way of doing things

To attain it's peak performance bun has 2 main ingredient's

- Global Module Cache: The system stores copies of software modules that has previously downloaded from the npm registry. Instead of repeatedly downloading the same packages it stores in global module cache and uses them when ever needed. `pnpm to uses this method`

- Fastest System Calls: System calls are fundamental functions provided by the operating system that allow programs to interact with the hardware. It optimizes its interactions with the underlying operating system to ensure that it performs tasks as efficiently as possible.

> Its possible to use BUN as a package manager even when its not used as a runtime

### Bundler"📦

Bun is a JavaScript and TypeScript bundler and minifier that can be used to bundle code for the browser, Node.js, and other platforms. It's heavily inspired by esbuild and provides a compatible plugin API.

`bun build ./index.tsx --outdir ./build`

> Bun's fast native bundler is now in beta

Bun introduces JavaScript Macros are used to make a sequence of computing instructions available to the programmer as a single program statement. Macros runs JavaScript functions at bundle-time. The value returned from these functions are directly inlined into the bundle.

### Testing libraries"🧪

Bun has a built-in testing module bun:test that is fully Jest-compatible . Tests are executed with the Bun runtime

It supports:

TypeScript and JSX

- Snapshot testing
- Lifecycle hooks
- UI & DOM testing
- Watch mode with --watch
- Script pre-loading with --preload

Tests are written in JavaScript or TypeScript with a Jest-like API. Refer to [Writing tests](https://bun.sh/docs/test/writing) for full documentation.

### Installing bun:

Mac/Linux/WSL ( Windows Subsystem for Linux ) 

CURL:

`curl -fsSL https://bun.sh/install | bash`

NPM:

`npm install -g bun`

- Windows : Sadly windows user's cant able to experience bun at the moment as it is in highly experimental phases. Join #windows channel on BUNs Discord for updates.

> Using WSL ( Windows Subsystem for Linux ) we can install and run BUN in window's