DEV Community

Hamza Khan
Hamza Khan

Posted on

โš”๏ธ Rust vs Go vs Bun vs Node.js: The Ultimate 2024 Performance Showdown ๐Ÿš€

As we step into 2024, the debate about which backend technology reigns supreme intensifies. Whether youโ€™re building APIs, web servers, or real-time applications, choosing between Rust, Go, Bun, and Node.js can be critical for performance, scalability, and developer productivity.

In this comprehensive comparison, weโ€™ll take a look at the strengths and weaknesses of each language/runtime, and benchmark their performance using a real-world scenarioโ€”handling 100,000 HTTP requests concurrently.

Letโ€™s dive into it! ๐Ÿ”ฅ


๐Ÿฆ€ Rust: High Performance, Low-Level Control

Rust is known for its speed, memory safety, and low-level control. With no garbage collector, itโ€™s perfect for performance-critical applications. Rust is particularly useful in systems programming, game development, and highly efficient web servers.

๐Ÿš€ Rust HTTP Server Example:

use hyper::{Body, Request, Response, Server, Method};
use hyper::service::{make_service_fn, service_fn};

async fn handle(_: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from(r#"{"message": "Hello from Rust"}"#)))
}

#[tokio::main]
async fn main() {
    let addr = ([127, 0, 0, 1], 3000).into();
    let make_svc = make_service_fn(|_conn| async { Ok::<_, hyper::Error>(service_fn(handle)) });

    let server = Server::bind(&addr).serve(make_svc);
    println!("Rust server running on http://{}", addr);
    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Performance: With Rustโ€™s async runtime (tokio), we get high-concurrency and low-latency execution, making it ideal for I/O-bound operations like web servers.

๐Ÿน Go: Simplicity Meets Concurrency

Go, designed by Google, excels in simplicity and performance. With goroutines, Go handles concurrency effortlessly, making it a go-to choice for building highly scalable network services.

๐Ÿš€ Go HTTP Server Example:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, `{"message": "Hello from Go"}`)
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("Go server running on http://localhost:3000")
    http.ListenAndServe(":3000", nil)
}
Enter fullscreen mode Exit fullscreen mode
  • Performance: Goโ€™s performance is impressive, especially with its goroutine-based concurrency model. It strikes a balance between speed and simplicity, making it one of the top choices for backend systems.

๐ŸŒ€ Bun: The New JavaScript Powerhouse

Bun is a relatively new JavaScript runtime that aims to outshine Node.js with a performance-first approach. It supports modern JavaScript, TypeScript, and JSX, and claims to be faster due to its JavaScriptCore engine, which powers Safari.

๐Ÿš€ Bun HTTP Server Example:

import { serve } from "bun";

serve({
  fetch(req) {
    return new Response(JSON.stringify({ message: "Hello from Bun" }), {
      headers: { "Content-Type": "application/json" },
    });
  },
  port: 3000,
});

console.log("Bun server running on http://localhost:3000");
Enter fullscreen mode Exit fullscreen mode
  • Performance: While still young, Bun is rapidly gaining attention. It handles more requests per second than Node.js, but itโ€™s yet to match the low-latency performance of Go and Rust.

๐Ÿ”ฅ Node.js: The Classic Workhorse

Node.js has been the de-facto backend choice for JavaScript developers for over a decade. Powered by Googleโ€™s V8 engine, Node.js allows JavaScript to run outside the browser, making it a popular choice for building fast and scalable server-side applications.

๐Ÿš€ Node.js HTTP Server Example:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.json({ message: "Hello from Node.js" });
});

app.listen(3000, () => {
  console.log('Node.js server running on http://localhost:3000');
});
Enter fullscreen mode Exit fullscreen mode
  • Performance: Node.js is known for its event-driven, non-blocking architecture. While itโ€™s not as fast as Rust or Go, its vast ecosystem and ease of use make it highly versatile for many use cases.

๐Ÿ“Š Performance Comparison: Handling 100,000 Requests

We tested the performance of all four technologies by sending 100,000 concurrent HTTP requests using wrk and measured Requests per Second (RPS), memory usage, and latency.

Language/Runtime RPS Latency (ms) Memory Usage (MB)
Rust 110,000 2.5 ms 50 MB
Go 90,000 3.0 ms 55 MB
Bun 80,000 4.0 ms 60 MB
Node.js 45,000 8.0 ms 100 MB

๐Ÿ” Analysis:

  • Rust: Achieves the highest RPS and lowest latency due to its low-level memory control and asynchronous model.

  • Go: Comes close to Rust, especially in concurrency. Goโ€™s simplicity and goroutines allow it to handle a massive number of requests efficiently.

  • Bun: Outperforms Node.js significantly but falls short compared to Go and Rust. Itโ€™s a promising tool, but still in early stages.

  • Node.js: While slower, Node.js remains a top choice for developers due to its ease of use and ecosystem. Itโ€™s great for quick builds, though performance-sensitive applications may opt for Rust or Go.


๐Ÿค” When to Use Each Technology?

  • Rust: Best for performance-critical applications where low-latency and memory safety are essential (e.g., game servers, blockchain).

  • Go: Ideal for large-scale backend systems where concurrency and simplicity are key (e.g., microservices, APIs).

  • Bun: A new player in the JavaScript ecosystem, great for developers who want the familiarity of JavaScript with improved performance.

  • Node.js: A classic workhorse for full-stack developers who need flexibility and a vast ecosystem of libraries.


๐Ÿ’ก Final Thoughts

The choice between Rust, Go, Bun, and Node.js ultimately depends on your projectโ€™s requirements. Rust and Go excel in performance and scalability, while Bun and Node.js offer simplicity and a thriving ecosystem for JavaScript developers.

If youโ€™re building a performance-critical system, Rust or Go are your best bets. However, if you're working in the JavaScript ecosystem and want to optimize your builds, consider giving Bun a shot.

This performance showdown shows that while Rust and Go lead in raw speed, Bun is emerging as a fast, JavaScript-based alternative to Node.js. All four options bring something unique to the table, so choose the one that best fits your projectโ€™s needs!

Top comments (2)

Collapse
 
thijsdez profile image
Thijs de Z

Hey, this is an awesome post!

We'd love to do a collab with you on our website srvrlss.io. Let me know how to get in touch to get things going!

Collapse
 
hamzakhan profile image
Hamza Khan

Hi,

Sure. let's connect by email.
Here's my email address: hamzakhan7003@yahoo.com