Bun vs Go: Native hello world performance

Mayank Choubey
Tech Tonic
3 min readNov 8, 2023

--

In this concise piece, we delve into a head-to-head performance comparison of native servers offered by Bun and Go, focusing on the classic ‘Hello World’ benchmark. This topic has piqued the interest of our readers, prompting multiple requests for an in-depth exploration, especially after Deno has an improved native server since the last benchmarking.

Setup

We conducted all tests on a MacBook Pro M2 with 16GB of RAM, using specific software versions:

  • Bun v1.0.9
  • Go v1.21.3

To generate HTTP load, we utilized the Bombardier test tool. Below is the application code we used:

Bun

Bun.serve({
port: 3000,
fetch(req) {
try {
const pathName = new URL(req.url).pathname;
if (pathName !== "/") {
return new Response(null, { status: 404 });
}
return new Response("Hello world");
} catch (e) {
return new Response(null, { status: 500 });
}
},
});

Go

package main

import (
"io"
"net/http"
)

func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":3000", nil)
}

func helloWorld(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
}

You might be curious why Bun code has more lines than Go. This is because Bun’s built-in server lacks routing capabilities.

Results

All tests are executed for 10 million requests using 50, 100, and 300 concurrent connections. The results in chart and tabular form are as follows:

Conclusion

Bun does give a very tough competition to Go. Surprisingly, in a basic hello world scenario (which is far from reality), Bun’s built-in server is faster than Go’s. Go uses a lot of CPU, whereas the memory usage is about the same for both.

Thanks for reading!

--

--