BUN

Bun is a new JavaScript runtime, similar to Node.js or Deno. Unlike Node.js or Deno, however, Bun is built using WebKit’s JavaScriptCore instead of Chrome’s V8 engine. Designed as a drop-in replacement for Node.js, Bun is a single binary (written in Zig) that acts as a bundler, transpiler and package manager for JavaScript and TypeScript applications. Bun is currently in beta, so expect bugs or compatibility issues with a few Node.js libraries. However, it’s been built from the ground up with several optimizations, including fast startup and improved server-side rendering.


Bun is a new:

  • JavaScript runtime with Web APIs like fetch, WebSocket, and several more built-in. bun embeds JavaScriptCore, which is typically quicker and more memory-friendly than more widely used engines like V8(though harder to embed)
  • JavaScript/TypeScript/JSX transpiler
  • JavaScript & CSS bundler
  • Task runner for package.json scripts
  • npm-compatible package manager
  • All-in-one quick and user-friendly tool. You only need bun for development rather than 1,000 node modules. Bun is an experimental software. Today, bun’s primary focus is bun.js: bun’s JavaScript runtime.

Bun is a modern JavaScript runtime like Node or Deno. It was built from scratch to focus on three main things:

  • Start fast (it has the edge in mind).
  • New levels of performance (extending JavaScriptCore, the engine).
  • Being a great and complete tool (bundler, transpiler, package manager).

Bun is designed as a drop-in replacement for the current JavaScript & TypeScript apps or scripts — on the local computer, server or on the edge. Bun natively implements hundreds of Node.js and Web APIs, including ~90% of Node-API functions (native modules), fs, path, Buffer and more.

Its goal is to run most of the world’s JavaScript outside of browsers, bringing performance and complexity enhancements to the future infrastructure, as well as developer productivity through better, simpler tooling.

How does Bun work?

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

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

Why is Bun fast?

A long period of time spent profiling, benchmarking and optimizing things. The answer is different for every part of Bun, but one general theme: ZIG’s low-level control over memory and lack of hidden control flow makes it much simpler to write fast software.

Documentation
As it is still early beta Bun.js doesn’t have a lot of documentation about it and its internals.

Fast start time
Bun has a better start time than Node.js, which can be a great benefit in a Serverless environment, or PC applications built with something like Electron.

No Debugger
It actually does not have a debugger yet.

Taking Bun for a test drive

To get started with Bun, first, we’ll have to install it. According to the Bun documentation, installation requires only the following command:

curl https://bun.sh/install | bash

Keep in mind, this command will only work on Mac and Linux. So, if you’re using Windows, you’ll need to set up Window Subsystem for Linux to install Bun.

Once it’s done installing, make sure to read the confirmation prompt with directions for adding Bun to your PATH. It will require you to add the following lines to your .bashrc or .zshrc files:

BUN_INSTALL="/home/<username>/.bun"
PATH="$BUN_INSTALL/bin:$PATH"

Now, if you run bun–version, you should get a version number printed confirming you have installed it correctly.

Oven will provide incredibly fast serverless hosting & continuous integration for backend & frontend JavaScript apps — and it will be powered by Bun.
It will support popular frontend frameworks like Next.js, Vite, SvelteKit, SolidStart and many more — along with backend frameworks like Express, Fastify, NestJS and more.
The plan is to run our own servers on the edge in datacenters around the world. Oven will leverage end-to-end integration of the entire JavaScript stack (down to the hardware) to make new things possible.

Bun vs Node.js

Bun asserts that it is significantly quicker than Node.js and Deno. With Bun, a straightforward programme like hello world functions much more quickly. The same applies to running tests, installing npm, etc. The key to Bun is speed! The main USP for Bun will be this. Run the exact same programme with improved performance!

Bun claims that they are many times faster than both Node.js and Deno. Deno claims that they are faster than Node.js. We need independent runs to verify the claims.
Code
The hello world code in both runtimes uses native APIs only.

Bun

export default {
 port: 3000,
 fetch(request) {
 return new Response("Hello world!");
 },
};

Node.js

const http = require("http");
http.createServer((_, resp) => {
 resp.writeHead(200, {
 "content-type": "text/plain",
 });
 resp.end("Hello world");
}).listen(3000);

It’s not just about the runtime’s speed. A runtime may be extremely quick, but it could also cause the system to halt if it consumes too much CPU, memory, disc, etc. The majority of Bun’s official website’s measurements are requests per second. This doesn’t represent the complete picture. Additionally, we need to evaluate how response times vary among quantiles.

Here is the complete list of measurements:

  • Minimum response time
  • 10th percentile response time
  • 1st quartile response time
  • Mean response time
  • Median response time
  • 3rd quartile response time
  • 90th percentile response time
  • Maximum response time
  • CPU usage
  • Memory usage

Results

This section contains the results for the load tests carried to find out how Bun performs when compared to Node.js for a simple hello world use case. The results are detailed enough to understand all the aspects, not just requests per second.

50 concurrent connections

Let’s see how Bun performs in front of Node.js for a low concurrency of 50 connections.

Conclusion :-

Bun is a runtime developed in the Zig programming language, which also embraces web standards but aims for compatibility with Node.js APIs, so developers can easily migrate existing code.One of the most interesting choices is that Bun uses the JavaScriptCore as its engine, unlike Node.js and Deno, which use V8. The result is a blazing fast runtime that also offers several quality of life features for JavaScript developers.Bun also has first class integration of TypeScript and JSX. It aims to provide many of the features of transpilers, like Babel, and Bundlers like Webpack, Rollup, Snowpack, and Vite.

With Bun, a lot of things might get better about JavaScript. It’s probably time for JavaScript to gain better adoption in performance-critical fields like AI/ML, game development, etc.

 

For more details contact info@vafion.com

Follow us on Social media  : Twitter |  Facebook | Instagram | Linkedin

Similar Posts:

    No similar blogs

Related Posts

Stay UpdatedSubscribe and Get the latest updates from Vafion