Skip to main content

Bun, a nova runtime JavaScript

· 4 min read
Bruno Carneiro
Fundador da @TautornTech

Bun!


Have you heard of Bun?

Bun is a new JavaScript runtime developed to be a drop-in replacement for Node.js. It natively implements hundreds of Node.js and Web APIs, including fs, path, Buffer, and much more.

Bun is a toolkit for JavaScript and TypeScript that enables the creation of web applications, CLIs, and much more.

The Three Main Development Goals

1 - Speed: Bun was developed to be fast. It was extended from JavaScriptCore, initially developed for Safari.

2 - Elegance: Bun provides a minimal pre-configured package of highly optimized APIs for developing common tasks, such as starting an HTTP server and writing files.

3 - Cohesive DX: Bun is a complete toolkit for JavaScript app development, including package management, test runners, and bundlers.

Bun Highlights

  • Drop-in Node.js compatibility: Bun aims for full compatibility with the Node.js API. Most npm packages intended for Node.js environments will work with Bun out of the box; the best way to know for sure is to try it.

  • Works with node_modules: With Bun, it is still possible to use node_modules and package.json to manage dependencies.

  • Web-standard APIs: Bun has implemented some standard APIs that are widely used, such as fetch, ReadableStream, Request, Response, WebSocket, and FormData.

  • Fast running performance: Bun inherited the JavaScriptCore engine, the engine created for Safari, with native speed features built in Zig.

  • No more hassle: There is no need to understand how CommonJS, ESM, file extensions, resolution priority, and package.json configurations work. Bun just works.

  • JSX: JSX works perfectly. Bun internally transpiles JSX syntax to vanilla JS, like TS. Bun supports React by default but respects custom JSX transform defined in tsconfig.json.

  • TypeScript: TS by default, able to run .ts and .tsx files without additional configuration, respecting tsconfig.json.

  • Watch mode: The bun run CLI provides a --watch flag that automatically restarts the application when a file is modified.

Benchmark

Comparison with Node.js and Deno


Server-side rendering


Benchmark



Websocket chat server


Benchmark



Query Data


Benchmark



Installation

curl -fsSL https://bun.sh/install | bash # for macOS, Linux, and WSL

Learn more

QuickStart

Create a hello-bun folder and enter it:

mkdir hello-bun && cd hello-bun

Run the command $ bun init

At the end of the process the initial configuration files and an index.ts file will be generated.

Open the index.ts file and replace the content with:

const server = Bun.serve({
port: 3000,
fetch(req) {
return new Response("Bun!");
},
});

console.log(`Listening on http://localhost:${server.port} ...`);

Run in the terminal $ bun index.ts

A message will appear: Listening on http://localhost:3000 ...

This indicates that the server is ready and running on port 3000.

info

bun run is approximately 28x faster than npm run (6ms vs 170ms of overhead).

Learn more

Conclusion

The goal of Bun is to run most server-side JavaScript applications and provide tools for performance, reducing complexity and increasing developer productivity.

At first glance the tool looks very promising, but I don't believe it will replace Node — it is too early to say anything like that. Node is already quite robust, has been on the market for 14 years with thousands of applications in production and a massive community. Bun is taking its first steps, just like Deno.

I don't have a strong opinion on Bun yet, but I believe it is worth following the project and seeing how it evolves. Over time I will bring news and tests with the tool.

But to know whether it is good or not, you just have to test it and see how it behaves. However I do not recommend using it in production yet — it is still too early for that and v1.0 was released recently.

But it is great to see new tools emerging and bringing new possibilities for JavaScript application development.

References

Bun