Is Bun A Drop In Replacement

This will be a brief discussion about bun being a replacement

Bun: A Fast and Comprehensive Toolkit

A wise man once told me, "When you start eating Bun, Node.js will feel bland".

Bun is a fast and comprehensive toolkit for JavaScript and TypeScript development. It is designed to handle various tasks involved in the development process, including running, building, testing, and debugging applications. Bun aims to provide a streamlined experience by allowing developers to work with JavaScript and TypeScript projects of different scales. Whether you're working on a small single-file project or a larger full-stack application, Bun aims to be a versatile tool that can handle the necessary tasks efficiently.

Naming Issue - Theo

The creator of T3, Theo, warned them about not using the name "buntime," but it seems they didn't heed the advice. This decision had consequences, as Bun became a compiler, package manager, and bundler.

What Makes Bun Different?

What sets Bun apart from other primitive JavaScript runtimes? The goal of Bun is to eliminate slowness and complexity while retaining the great features of JavaScript. It ensures that your favorite libraries and frameworks continue to work, and you don't need to unlearn the conventions you're already familiar with.

One notable difference is that Bun drops the use of Node.js's primitive JavaScript engine, Chrome V8, and instead utilizes the JavaScript core engine (Apple), which is optimized for startup time. Additionally, as a YC-funded company, Bun's long-term sustainability may be a concern, making it challenging for Jarred, the creator of Bun, to pay developers and recruit more creative developers to the team except for OSS guys.

Furthermore, while the camel was wandering with me, it shared a piece of advice: to use OCaml for building such runtimes and compilers due to its functional and powerful type safety features. However, it seems Zig was chosen instead, as Zig made the development process a "bun-dle" of joy, while OCaml and C++ couldn't "knead" up the same level of fun! Rest assured, Jarred Sumner was present during the camel's storytelling—just kidding! 😆

Behind the Drama: Exploring the Bun Ecosystem

Zig covers over 43% of Bun's codebase, alongside C++, both being performant choices for building runtimes and compilers. However, let me share a secret: ThePrimeage, a somewhat average Rust user, is quite pissed off. This doesn't necessarily mean Zig is faster than the C++ on which Node.js is built. Also, keep in mind C++ is fast, but that doesn't mean any code written in C++ will be. Perhaps I'm more sensitive to the peculiar friction within the Node.js space. So, let's dive deeper into what's happening behind this drama.

Test Runner and Development Tools

  • Is the built-in test runner in Bun legitimate, or do you need something else?
  • What are people using nowadays? Is npm still the go-to package manager, or should you consider Yarn or pnpm?
  • what about workspaces? Do people still rely on ts-node, or does nodemon suffice, even though it's not well-documented? Do you still need ts-watch or node-watch?

tsconfig and package.json

  • What new additions have been made to tsconfig?
  • Should you start using modules yet? Are there any new fields in package.json,
  • what should you be doing with the "targets" field if you're building a library? Should you stick with tsc, or is there a better alternative that everyone is using?

Express and ORM/Query Builders

  • Is Express 5 already out? Just kidding! Well, the beta version seems to work, so can you just use that?
  • speaking of which, what's the new ORM/query builder/driver du jour? Is the one you used a few months ago considered abandonware? Can you opt for something different? Is it production-ready?

Bun's Solutions

Surprisingly, all these questions are answered by Bun. Bun provides access to a faster runtime, which enhances the entire ecosystem that our projects rely on. It significantly speeds up test suites, bundlers, linters, and all the developer tooling that supports modern JavaScript development.

Let me ask you something: Do you consider the database as a bottleneck? While performing queries doesn't block the main thread, running JS does. However, performance is definitely a selling point for the Bun team. It's a way to provide the Node.js community with a better experience and address the performance obsession.

What about the network? Can that be a bottleneck too? Both caching mechanisms are employed. The great thing about Bun is that it doesn't check the network when installing packages. It relies on the oldy versions, possible number that can be represented by the caret (^) symbol, and it also ignores the latest package versions (@latest).

Some argue that all these benchmarks and comparisons are nitpicks and often unfair. The true value Bun offers is an improved developer experience (DX) with built-in TypeScript support.

Now, I'm curious to hear your thoughts. What do you think about all of this?

What Bun Replaces in Node.js

  • Bunx: Bunx is five times faster than npx and eliminates the need for modules like dotenv and cross-env. It provides faster dependency installation and includes a default watch mode with websocket support.

  • Transpiler: Bun can run various file types such as .js, .ts, .cjs, .mjs, .jsx, and .tsx. This eliminates the need for tools like esbuild, Webpack, Parcel, and Rollup.

  • Node.js Compatibility: Bun aims for complete compatibility with the Node.js API. Most npm packages intended for Node.js environments can work seamlessly with Bun. Check the list of supported Node.js built-in modules on the Bun documentation.

  • APIs: Bun implements a set of native APIs on the Bun global object and through built-in modules. These APIs are highly optimized and introduce new APIs primarily for server-side tasks where no standard exists. See the list of supported Bun APIs on the Bun documentation.

  • Package Manager: Bun is an npm-compatible package manager that reads your package.json and writes to node_modules.

  • Testing: Bun is Jest compatible, providing snapshot testing, mocking, and code coverage.

Bun also offers ESM and CommonJS compatibility, allowing you to use both module systems within the same file without complex configuration.

For more information, you can visit the Bun documentation links provided.

  • Like yarn, npm, and pnpm, Bun acts as a package manager focused on saving disk space and boosting installation speed. Starting today, Projects that contain a bun.lockb file will automatically run bun install as the default Install Command using bun@1. Bun is a fast JavaScript runtime. Its goal is to make the experience of building software faster, less frustrating, and more fun. Bun has been benchmarked in the cloud as well as locally, and it outperforms the Node.js runtime. It is on par with other server-side languages like Rust in terms of performance. For example, when benchmarked using wrk:

Bun is a fast JavaScript runtime. Its goal is to make the experience of building software faster, less frustrating, and more fun. Bun has been benchmarked in the cloud as well as locally, and it outperforms the Node.js runtime. It is on par with other server-side languages like Rust in terms of performance. For example, when benchmarked using wrk:

The source code for this test can be found at: github

Bun Benchmark

  ~ wrk -t16 -c1000 -d30 http://IP_ADDRESS:3000                                         [23/09/11|06:20pm]
Running 30s test @ http://IP_ADDRESS:3000
  16 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    42.61ms   15.20ms 647.53ms   95.99%
    Req/Sec     1.48k   173.54     2.17k    80.20%
  707302 requests in 30.10s, 88.36MB read
  Socket errors: connect 0, read 83, write 0, timeout 0
Requests/sec:  23497.18
Transfer/sec:      2.94MB

Nodeje Benchmark

  ~ wrk -t16 -c1000 -d30 http://IP_ADDRESS:3000                                         [23/09/11|06:21pm]
Running 30s test @ http://IP_ADDRESS:3000
  16 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    52.50ms   77.15ms   2.00s    98.27%
    Req/Sec     1.23k   235.30     2.54k    78.12%
  587719 requests in 30.10s, 91.92MB read
  Socket errors: connect 0, read 0, write 0, timeout 434
Requests/sec:  19524.73
Transfer/sec:      3.05MB

Some other benchmarks record

Results

For results suffix with -node means that the framework is run in Node, otherwise is using Bun.

These results are measured in req/s:

FrameworkAverageGet (/)Params, query & headerPost JSON
uws (node)369,192.103457,538.99389,308.63260,728.69
stricjs (bun)268,422.07323,535.76262,470.15219,260.3
bun (bun)262,250.81325,012.5238,600.42223,139.51
elysia (bun)261,538.61321,378.82248,866.5214,370.51
vixeny (bun)260,097.983321,361.76250,442.26208,489.93
hyper-express (node)241,177.05350,373.13274,099.1299,058.9
hono (bun)238,294.85295,794.19227,806.78191,283.58
bun-web-standard (bun)233,734.623282,952.77220,206.67198,044.43
nhttp (bun)232,273.04300,289.39213,002.07183,527.66
hyperbun (bun)162,952.447218,591.35158,992.25111,273.74
nbit (bun)149,914.61190,477.31147,009.36112,257.16
baojs (bun)148,991.963187,599.82144,528.44114,847.63
hono (deno)132,527.407167,513.6137,757.2892,311.34
h3 (node)109,805.423134,438.5597,886.9397,090.79
fast (deno)94,872.417111,352.9594,498.7278,765.58
cheetah (deno)65,816.437123,169.8256,127.7618,151.73
fastify (node)65,813.13778,048.6867,553.9951,836.74
oak (deno)49,623.60358,177.1450,212.6440,481.03
abc (deno)42,806.14354,345.3945,928.6528,144.39
koa (node)39,195.1845,212.3440,435.1231,938.08
express (bun)29,432.23738,914.3633,669.5415,712.81
hapi (node)28,066.04742,783.6715,394.8126,019.66
adonis (node)23,047.522,368.9121,316.6725,456.92
express (node)16,791.03718,300.9217,711.0314,361.16
hono (node)15,607.2817,964.7216,495.5212,361.6
nest (node)15,139.23317,054.9715,822.3712,540.36
acorn (deno)3,217.675,340.932,452.291,859.79

See more detail in results

Conclussion

Despite the impressive performance of Bun, it's important to note that it's not necessary to replace your current tech stack with Bun. Both Bun and Node.js have their strengths and are capable of fulfilling their respective roles effectively. It's possible that Node.js is working hard to keep up with the demands of the ecosystem. Ultimately, the choice of technology depends on your specific requirements and preferences.

For more information, you can visit the Bun documentation links provided.