banner image 1 banner image 2

Bun — JavaScript just got faster

November 18, 2022
4 mins
command
blog-img 1
Muhammediqbal P
Author

This article helps you to have a basic understanding of Bun.

By Muhammediqbal P— A Technophile .


Bun — JavaScript
Bun-JavaScript

What is Bun?

Bun is built on Zig and has the ability to transpile, set up, and run TypeScript and JavaScript projects. It is a complete utility since it also functions as a package manager. That’s why it’s called all-in-bun ( bundler, transpiler, package manager). This has been made possible via the usage of Zig, a dated programming language that was initially created for video games. It also includes ~90% of Node-API functions (native modules), fs, path, Buffer, and more.

What makes Bun special?

Unlike Deno, Bun is intended to be a drop-in replacement for Node, Webpack, Babel, Yarn, and Post CSS — all in one neat package.

https://gist.github.com/ikku47/4a1e22b8d64bb6ce784005cc92c65d6f#file-deno-node-bun
  • Fast built-in sqlite3 module (MySQL and PostgreSQL are also planned).
  • Out-of-the-box .env.toml, and CSS support (no extra loaders required).
  • Built-in framework support and optimizations for React and Next.js
  • Built-in Foreign Functions Interface (FFI) for low-level calls to ABI-supporting languages such as C, Rust, or Kotlin.
  • Option to copy errors as Markdown (for rapid sharing).

How does Bun work?

Bun uses the JavaScriptCore engine, which tends to start and perform a little faster than more traditional choices like V8 ( Nodejs, Denojs ). The Bun is written in, a low-level programming language with manual memory management.

Bun — JavaScript

The Bun is written from scratch including the JSX/TypeScript transpiler, npm client, bundler, SQLite client, HTTP client, WebSocket client, and more.

How to set it up?

  1. Run this install script in your terminal to install Bun and we are ready to go.
Supports: macOS x64 & Silicon, Linux x64, Windows Subsystem for Linux (WSL)
curl -fsSL https://bun.sh/install | bash

2. Now it’s Hello World time 😊, Create an http.js file in your project folder:

https://gist.github.com/ikku47/fedfa07d0f4d2cfde16a4133e525c6ac#file-bun-hello-word-js

3. Now let’s run it :

bun run http.js

That’s it, you have your first Bun project ready to go.

As per Bun benchmark testing for version 0.0.76

4. Shall we try a react project now, let’s add react and react-dom to set it up.

bun add react react-dom
https://gist.github.com/ikku47/828e1071d4d53331a2dc60b9cc7a1639#file-bun-react-js

Now you have your react project using Bun.

5. Now let’s try to print the contents of a file

https://gist.github.com/ikku47/409f6a2db25abee6de885e14e09e7d31#file-bun-cat-js

If you need more examples, have a look at this one.

So, Is it fast?

Is it as fast as it claims to be? Let’s look at some benchmark results.

Server-side rendering — React
Server-side rendering — React
Web socket Server Chat
Web socket Server Chat
Bun SQLite table
Bun SQLite table
Bun is twice as fast as Node and 1.7 times faster than Deno for serving HTTP requests
Bun is twice as fast as Node and 1.7 times faster than Deno for serving HTTP requests.

You can have more bench-marking and detailed reports on this blog by
Jose Granja on this medium article.

Still, why not Bun?

  • Not Production Ready.
  • The scope of the project is very large.
  • Lack of information and documentation.

Conclusion

Not only is Bun fast, but it is also blazing fast. It feels like you can do anything in under a second. Bun.js focuses on performance, developer experience, and compatibility with the JavaScript ecosystem

Will Bun replace Node Js and Deno Js? It’s too early to say as it is still in beta. Bun JavaScript aims for compatibility, so it has a better chance than Deno. And, as we’ve seen, even at this early beta stage it can be a very efficient and fast tool.

You can follow for more updates on Bun on their Discord channel, and the main developers’ Twitter handle.

References :

[embed]https://github.com/oven-sh/bun#Reference[/embed][embed]https://github.com/oven-sh/bun#Reference[/embed][embed]https://github.com/oven-sh/bun#Reference[/embed][embed]https://github.com/oven-sh/bun#Reference[/embed]

Meet the team!

Author

Muhammediqbal P

Reviewed By

Sureshkumar M

Editor

Seema Jain


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P