%% generate tags start %%
#software-engineering
%% generate tags end %%
#software-engineering/bun #software-engineering/typescript
> [!info] Official site
> [What is Bun? | Bun Docs](https://bun.sh/docs)


## How to Use?
```
bun run start # run the `start` script
bun install <pkg> # install a package
bun build ./index.tsx # bundle a project for browsers
bun test # run tests
bunx cowsay "Hello, world!" # execute a package
```
## How to Start a Server with Bun?
```ts
Bun.serve({
fetch(req) {
return new Response(`Bun!`);
},
});
```
> [!info] see more
> [HTTP server – API | Bun Docs](https://bun.sh/docs/api/http)
## How to Deploy Bun?
deploy on northflank: [Deploying a Bun app on Northflank — Northflank](https://northflank.com/guides/deploying-a-bun-app-on-northflank)
deploy on fly: [Fly.io ❤️ Bun · The Fly Blog](https://fly.io/blog/flydotio-heart-bun/)
## Testing with Bun
Bun ships with a fast built-in test runner. Tests are executed with the Bun runtime, and support the following features.
- TypeScript and JSX
- Lifecycle hooks
- Snapshot testing
- UI & DOM testing
- Watch mode with `--watch`
- Script pre-loading with `--preload`
```
bun test
```
The runner recursively searches the working directory for files that match the following patterns:
- `*.test.{js|jsx|ts|tsx}`
- `*_test.{js|jsx|ts|tsx}`
- `*.spec.{js|jsx|ts|tsx}`
- `*_spec.{js|jsx|ts|tsx}`
```ts
// math.test.ts
import { expect, test } from "bun:test";
test("2 + 2", () => {
expect(2 + 2).toBe(4);
});
```
To learn more: [bun test – Test runner | Bun Docs](https://bun.sh/docs/cli/test)
## SQLite Database with Bun
Bun natively implements a high-performance [SQLite3](https://www.sqlite.org/) driver. To use it import from the built-in `bun:sqlite` module.
```ts
import { Database } from "bun:sqlite";
const db = new Database(":memory:");
const query = db.query("select 'Hello world' as message;");
query.get(); // => { message: "Hello world" }
```
## Why Bun is so Good?
> [!info] see more
> [Unleashing the Speed: Exploring the Power of Bun.js and the Future of JavaScript Runtimes | by Vaishnav Manoj | DataX Journal | Medium](https://medium.com/data-science-community-srm/unleashing-the-speed-exploring-the-power-of-bun-js-and-the-future-of-javascript-runtimes-a689ff274952)
> What makes Bun stand out from the crowd is its **three** major goals:
- To execute JavaScript at a **BLAZINGLY FAST SPEED** 🚀
- To provide complete and lightning-fast tooling, including a **bundler** and **package manager**
- Be a **drop-in replacement** for all existing Node projects
> So how does Bun plan to achieve these goals even though it’s still in the [Oven](https://oven.sh/)?
- **Speed**: Bun is built on top of the [**JavaScriptCore**](https://developer.apple.com/documentation/javascriptcore) engine, which is the same engine that powers Safari and iOS. This gives Bun a significant performance advantage over other JavaScript runtimes, such as Node.js and Deno.
- **Tooling**: Bun comes with a built-in bundler, transpiler, and task runner. This means that you can use Bun to build, test, and deploy your JavaScript projects without having to install any additional tools.
- **Compatibility**: Bun is designed to be a drop-in replacement for Node.js. This means that you can use Bun to run existing Node.js projects without having to make any changes to your code.
Bun is still in development, but it’s already shaping up to be a powerful new JavaScript runtime.
> Here are some of the key features of Bun:
- **Speed**: Bun is **significantly faster** than Node.js and Deno, especially for large applications.
- **Memory usage**: Bun uses significantly **less memory than Node.js** and [Deno](https://deno.com/), even for large applications.
- **Ease of use**: Bun is **very easy to use**, even for beginners. It has a simple API that is similar to Node.js.
- **Documentation**: Bun is very [well-documented](https://bun.sh/docs), so it is easy to find help if you need it.
> How exactly does [JavaScriptCore](https://developer.apple.com/documentation/javascriptcore) fit into Bun?
> JavaScriptCore is a JavaScript engine that is used by Safari, Apple’s web browser.
Bun uses JavaScriptCore (JSC) as its JavaScript engine, while Chromium uses [V8](https://v8.dev/), Firefox uses [SpiderMonkey](https://spidermonkey.dev/), and Microsoft used Chakra for Internet Explorer but now uses V8 for Edge while [Hermes](https://hermesengine.dev/) is used for React Native.
JSC is generally faster than V8, especially for large applications. This is because JSC is written in C++, while V8 is written in JavaScript. C++ is a compiled language, which means that it is converted to machine code before it is executed. This makes C++ code faster than JavaScript code, which is interpreted at runtime.
**Bun is built on top of JavaScriptCore, which gives it a number of advantages over other JavaScript runtimes, such as Node.js and** [**Deno**](https://deno.com/)**.**