Bun v/s Go: Hello world performance comparison
Continuing my quest of performance comparisons, this article is about Bun and Go. Who do you think is faster for a simple hello world case? In the previous comparison of Deno vs Go, we found that Deno’s native server is faster than Go’s native server. If Deno turned out to be faster, then it’s easy to conclude that Bun will be even faster because Bun is a bit faster than Deno. This time, too, the comparison is with Go’s net/http. Let’s find out the answer, which we already know!
Setup
The test is executed on MacBook Pro M1 with 16G of RAM. The test is executed using the well-known HTTP tester: Bombardier. It’s interesting to know that Bombardier itself is written in Go using fasthttp. However, this article compares Bun’s native HTTP server (Bun.serve) with Go’s native server (net/http). Very soon, there will be follow-up articles comparing Deno and Bun with Go’s fasthttp server.
The test is executed for 10, 50, 100, and 300 concurrent connections.
The code is:
Bun
Bun.serve({
port: 3000,
fetch(req) {
try {
if (req.method !== "GET") {
return new Response(null, { status: 405 });
}
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!")
}
A total of 10M (10 million) requests are executed for each concurrency level.
The following measurements are taken:
- Time taken
- Requests per second
- Latencies: Mean, median, q25, q75, q90, maximum (in microseconds)
- System usage: Average CPU and memory usage
Results
Here are the charts representing results for each type of measurement (Note that all latencies are in microseconds):
This time I’m not surprised (and you’ll not be too!). Bun’s native server (Bun.serve) turns out to be quite faster than Go’s net/http server with comparatively less usage of the system resources. For 300 concurrent connections, Bun takes 57 seconds to finish off 10M requests, while Go takes 91 seconds. This is a big difference. Also, Bun uses half of Go’s CPU usage and the memory usage is almost the same.
Overall, Bun’s native server easily beats Go’s built-in native server.
UPDATE: As promised, the comparison with Go’s fasthttp is now available here.
More articles on similar topics can be seen in the magazine: The JS runtimes.