NestJS: Bun vs NodeJS

Gabriel Morishita - Sep 12 '23 - - Dev Community

What is Bun?

Bun JS or Bun Sh is an all-in-one toolkit for JavaScript and TypeScript, it offers a powerful toolkit with its CLI, it is currently available for Linux, MacOS and WSL users. To install it, run:

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

To see more of what Bun is and what it can do, check the official documentation.

Why doing that?

We all know that NodeJS deals with it pretty well, but Bun is a really promising deal, so why not?? We programmers love to test and see what happens, especially to try out new things!

Getting started

So now that we've got our bun CLI up and running, lets use nest CLI to generate two separate projects. They will be fully installed, built and run using both npm and bun so we can see the difference on first hand.

Note: all the benchmarking will be tested with wrk using Arch Linux x86_64 environment.

1. Dependency Install

After "nest newing" both projects, its time to see the first feature available on Bun, which is the package manager.

npm install
bun install

We can already see some improvement here! Bun install is blasting fast and wins the first challenge.

2. Development Server

We can't ignore the fact that a good development experience is a must have when it comes to frameworks. As both Bun and Node supports runtimes such as 'npm run start' we will have to see which one is the best when it comes to developing on real time. At the moment, NestJS is not using Bun.serve() to create a web server, so the comparisons we are making are all with the default NestJS provider.

Let's run both projects on different ports. Bun will be running on 3333 and Node will run on 4444 so we can easily see them acting.
Servers up

Without any change, let's compare them with wrk. Note: we are not running them concurrently at the same time, which means that the following results were achieved with all the computational power dedicated to one and only one test at a time.

http tests on dev server

We can see that Bun not only handles more requests per second (almost 2.4K requests more) but it has an average latency 30ms faster. This one is a solid win for Bun.

But this section ain't over, we still have something ALL developers love, and this is hot-reload. To enable hot-reload on NestJS using node, we need a service like nodemon to do that for us. Meanwhile on Bun we have the --watch arg that can be passed on bun start command. We have yet another type of hot-reload on Bun natively! But the watch key runs on file changes (which is the best for real time developers)

Bun hot reloader

Our little Chinese bread Bun is now 2x0 against the big guy NodeJS.

3. Testing

As we are too busy to write them from scratch, I guess we are going with the auto-generated test files from NestJS.

Test runners

WOOOOW, it seems NodeJS won't throw away the towel. Unlike the http requests battle, this one is by such a small difference that we don't feel comfortable using it. Our tiebreaker was expected to be the native test modules, but both NodeJS and Bun are loaded with a test runner, and as we are only considering Jest results, NodeJS can take this one and make it a 2x1 difference for Bun.

4. Build and deployment

Let's now build our giant Hello World NestJS application and see how it performs on raw using both "node dist/main.js" and "bun run dist/main.js" as well as the build time.

Build time

And another tiny difference on our comparison, only 500ms difference for Bun! This tiny differences can be justified by the fact that we are testing such a small project. It is worth mentioning though that this is not over! We still need to compare performance running compiled projects.

Builds running

When in doubt, benchmark it out!

http tests on build

And again a solid one for Bun, it's worth noticing that when dealing with compiled NestJS, Bun handles almost 10 thousand requests more then when running it on a development server, holly cow!

Also, we can now compare the development server and build server on both Node and Bun. While Bun cut the average latency by half and more then double requests per second. Node also shows some better results by reducing latency and passing Bun on max requests per second by almost 1k.

In general, Node performance is pretty decent, meanwhile Bun is just stunningly powerful. This was a really great test but Bun takes it on 3x1.

Conclusion

Overall, Bun can be really great on performance and deliver some amazing results. But we can't forget that it is currently on it's first version and can be very unstable on production, that's why I wouldn't recommend trading NodeJS by Bun on bigger projects. NodeJS is still the most reliable and trusted runtime environment to deal with JavaScript applications. However, Bun seems to be getting closer every step to take Node's crown. I was able to watch the early development of Bun since 0.3, times when running bun install would result on a segfault. Being able to see it grow it's own community and compete with giants like NodeJS is so nice.

Thanks for reading it, feel free to share your thoughts!!

. .
Terabox Video Player