Bun, the newest JS runtime in town!

Bun, the newest JS runtime in town!

Node.js has long been the go-to choice for developers since 2009 to build scalable server-side applications. However, the JavaScript ecosystem is constantly evolving, and new runtimes, offering unique features and advantages emerged in the last couple of years.
Deno(anagram for Node) 1.0 was released in 2020, promising greater security and in-built typescript. It is said to replace Node soon. But while we were expecting it, a new runtime landed, Bun!

What is Bun?

Bun is a JavaScript runtime designed for server-side and cloud-native applications. It's built with performance, security, and ease of use in mind. Bun is inspired by both Node.js and Deno and promises to improve performance, reduce complexity, and multiply developer productivity.
Bun is an all-in-one package for runtime, package management, bundling, and testing.

What makes it great?

  • Speed. Bun starts fast and runs fast. While Node.js uses Google's V8 engine that powers the Chrome browser, Bun uses JavaScriptCore, a performance-minded JS engine built for Safari. Bun leverages modern JavaScript features and optimizations to deliver excellent performance

  • Elegant APIs. Bun provides a minimal set of highly optimimized APIs for performing common tasks, like starting an HTTP server and writing files.
    It makes things simple!

      Bun.serve({
        port: 8080, // defaults to $BUN_PORT, $PORT, $NODE_PORT otherwise 3000
        hostname: "mydomain.com", // defaults to "0.0.0.0"
        fetch(req) {
          return new Response("404!");
        },
      });
    
  • Cohesive DX. Bun is a complete toolkit for building JavaScript apps. The goal is to build a cohesive, infrastructural toolkit for building apps with JavaScript/TypeScript, including a package manager, transpiler, bundler, script runner, test runner, and more.

That's cool and all, but what makes it impressive?

  1. Speed
    Bun stresses a lot on performance.

  2. In-built package manager

    Again, performance!

  3. Transpiler
    NodeJS doesn't support TS natively. We need an external dependency like ts-node to transpile code for you.

     npm i typescript ts-node --save-dev
    
     // package.json
     {
       "scripts": {
         "start": "ts-node ./src/main.ts"
       }
     }
    
     npm start
    

    With Bun, a JavaScript transpiler is integrated into the runtime, supporting .js, .ts, .jsx, .tsx files.

     bun main.ts
    
  4. Hot reloading
    Hot reloading is a developer savior while building, and debugging code locally. It refreshes code changes without requiring a restart. nodemon did it too, and the experimental --watch flag in Node was added.

    Bun has in-build support for this, using the --hot flag to run a file with hot reloading enabled. When any module or file changes, Bun re-runs the file.

     bun --hot run index.ts
    

There are many more Pros than Cons.

All in all, this looks promising and lives up to the hype. The question is how fast will devs adopt this. :)