日本語

How much faster is an Express app on Bun?

​​Bun​benchmark,Express,Node.js,TypeScript

An Express app written for Node.js also ran on Bun after I changed node app.ts to bun app.ts. I ran the same Express 5 app on both in a test environment. The responses were identical, and Bun handled more than 4 times as many requests per second.

Before you move an Express API server or mock server from Node.js to Bun, you can check which features work as they are, how Bun differs from Node.js, and the measured speed and memory use.

Note

The test environment was Ubuntu 24.04 (x86_64, 4 CPUs, Intel Xeon 2.30GHz), Bun 1.4.4, Node.js 22.23.4, and Express 5.3.0. I wrote the app in TypeScript and ran the .ts file directly on Node.js as well. Since Node.js 22.18.0, type stripping, which runs code by removing type annotations, has been enabled by default (TypeScript section of the Node.js official docs). For the speed tests, I pinned the server to one CPU core. I did not test Express 4, middleware such as template engines or sessions, configurations that use multiple CPUs (such as node:cluster), or Windows and macOS.

Running the same app on Node.js and Bun

The Bun official guide says that major Node.js HTTP libraries such as Express should work on Bun without changes. This is because Bun implements node:http and node:https, which these libraries use (Bun official Express guide).

To check this, I prepared an app.ts that uses common features: receiving JSON, serving static files, middleware, URL parameters, redirects, and handling errors thrown in async code. I installed the packages with npm install express @types/express and used the same node_modules for both Node.js and Bun.

ts
import express, { type NextFunction, type Request, type Response } from "express";

const app = express();
app.use(express.json());
app.use("/static", express.static("public"));

// Put the runtime name in a response header
app.use((_req: Request, res: Response, next: NextFunction) => {
  res.setHeader("X-Runtime", typeof Bun !== "undefined" ? `bun ${Bun.version}` : `node ${process.versions.node}`);
  next();
});

app.get("/", (_req, res) => {
  res.send("Hello World!");
});

app.get("/users/:id", (req, res) => {
  res.json({ id: Number(req.params.id), q: req.query.q ?? null });
});

app.post("/echo", (req, res) => {
  res.status(201).json({ received: req.body });
});

app.get("/redirect", (_req, res) => {
  res.redirect("/");
});

app.get("/async-error", async () => {
  throw new Error("非同期の処理で起きたエラー");
});

app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
  res.status(500).json({ error: err.message });
});

const port = Number(process.env.PORT ?? 3000);
app.listen(port, () => {
  console.log(`listening on ${port}`);
});

I started the app with node app.ts and then with bun app.ts, and sent requests to 7 URLs with curl -i. Here is part of the Bun output. Before comparing, I removed five headers: Date, ETag, Last-Modified, Keep-Alive, and Connection.

text
$ curl -i -X POST -H 'Content-Type: application/json' -d '{"name":"たま"}' http://localhost:3000/echo
HTTP/1.1 201 Created
X-Powered-By: Express
X-Runtime: bun 1.4.4
Content-Type: application/json; charset=utf-8
Content-Length: 30

{"received":{"name":"たま"}}

$ curl -i http://localhost:3000/async-error
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
X-Runtime: bun 1.4.4
Content-Type: application/json; charset=utf-8
Content-Length: 51

{"error":"非同期の処理で起きたエラー"}

When I compared the Node.js and Bun responses with diff, the only difference was the value of the X-Runtime header that I added. Apart from the five headers above, the status codes, the other headers, and the bodies were the same for all 7 URLs. An error thrown in an async function also reached the final error-handling middleware on both runtimes. This is a feature of Express 5. The changelog says the router now handles rejected promises (Express changelog).

text
$ diff check-node.out check-bun.out
4c4
< X-Runtime: node 22.23.4
---
> X-Runtime: bun 1.4.4

The other 5 cases showed only the same X-Runtime line as different.

The server returned by app.listen() does not inherit from net.Server

The Node.js compatibility page in the Bun docs says that node:http is "fully implemented," but it also notes that http.Server does not inherit from net.Server (Bun official docs, Node.js compatibility page). I checked this with the server returned by Express's app.listen().

ts
import express from "express";
import { Server as HttpServer } from "node:http";
import { Server as NetServer } from "node:net";

const server = express().listen(3000, () => {
  console.log("http.Server か:", server instanceof HttpServer);
  console.log("net.Server か:", server instanceof NetServer);
  server.close();
});
text
$ node server-type.ts
http.Server か: true
net.Server か: true
$ bun server-type.ts
http.Server か: true
net.Server か: false

The instanceof net.Server check returned true on Node.js but false on Bun. If your code checks for a server with instanceof net.Server, the result will differ on Bun. The same page also says that Bun ignores some listen() options, such as ipv6Only and signal.

Requests per second more than quadrupled

To compare speed, I prepared an Express app with no middleware (bench-app.ts). It has only two routes: /, which returns a string, and /users/:id, which returns JSON.

ts
import express from "express";

const app = express();
app.get("/", (_req, res) => {
  res.send("Hello World!");
});
app.get("/users/:id", (req, res) => {
  res.json({ id: Number(req.params.id), name: "tama" });
});
app.listen(Number(process.env.PORT ?? 3000));

For comparison, I also wrote servers that return the same responses without a framework: one with node:http, which I ran on both Node.js and Bun, and one with Bun.serve, Bun's built-in server API.

The test method was as follows. I pinned the server to CPU 0 with taskset -c 0. I ran wrk (3 threads, 50 concurrent connections) on CPUs 1 to 3 to generate load. Each run had a 3-second warm-up, followed by a 10-second measurement. I repeated this 3 times for 5 server setups and 2 paths.

text
$ taskset -c 0 bun bench-app.ts
$ taskset -c 1-3 wrk -t3 -c50 -d10s --latency http://127.0.0.1:3000/users/7

These are the results for /users/7, which returns JSON.

Server Run 1 Run 2 Run 3
Express (Node.js) 10,131 9,748 10,648
Express (Bun) 47,791 42,572 40,097
node:http (Node.js) 48,493 47,792 40,616
node:http (Bun) 73,273 74,827 77,038
Bun.serve 119,037 102,942 118,902

These are the results for /, which returns a string.

Server Run 1 Run 2 Run 3
Express (Node.js) 10,052 10,340 9,059
Express (Bun) 51,142 45,003 46,395
node:http (Node.js) 49,106 48,384 44,068
node:http (Bun) 91,234 81,419 82,474
Bun.serve 175,493 176,184 193,846

(Units are requests per second, rounded to whole numbers.)

Horizontal bar chart of requests per second for /users/7, which returns JSON. Median of 3 runs: Bun.serve 118,902; node:http (Bun) 74,827; node:http (Node.js) 47,792; Express (Bun) 42,572; Express (Node.js) 10,131
Requests per second for /users/7, which returns JSON (median of 3 runs)

The same Express app handled about 9,000 to 10,700 requests per second on Node.js and about 40,000 to 51,000 on Bun. Comparing medians, the Bun version was 4.2 times faster for /users/7 and 4.6 times faster for /. On Bun, Express handled about the same number of requests as the node:http version written without a framework, or slightly fewer.

Within Bun alone, the Express app handled a little under 60% of the requests that direct node:http handled. Compared with Bun.serve for /users/7 under the same conditions, it handled a little under 40%. In this test, rewriting the app with Bun.serve was faster than running Express on Bun.

The / case for Bun.serve uses a style that returns a Response created at startup, passing new Response(...) directly in routes. The official docs say this style is generally at least 15% faster than creating a Response on every request (Bun.serve routing page). So the Bun.serve number for / is not a fair comparison with the others under the same conditions.

Note

The numbers changed when I used a different load tool. With autocannon, which I tried first, Bun.serve handled about 44,000 to 59,000 requests per second. That is far below the wrk results of about 103,000 to 194,000. autocannon is a load tool that runs on Node.js. I did not specify workers, so it generated load on a single thread (autocannon README on workers). That is why the tables show the wrk results. With autocannon, Express handled 9,000 to 12,000 requests per second on Node.js and 57,000 to 80,000 on Bun. So Bun was still more than 4 times faster.

Response time and memory usage

In the same tests, I also recorded the response time (the value within which 50% and 99% of requests got a response) and the peak RSS memory used by the server process. These are the values for /users/7, which returns JSON.

Server 50% (median) 99% Peak memory
Express (Node.js) 3.53 to 3.85 ms 13.31 to 16.66 ms 153 to 154 MiB
Express (Bun) 0.94 to 1.05 ms 3.59 to 4.14 ms 76 to 78 MiB

With 50 concurrent connections, Bun's response times at both 50% and 99% were about a quarter of Node.js's. Bun also used about half the memory.

Startup time

I measured the time from starting the app to getting a response to the first request, five times each (bench-app.ts).

Runtime Run 1 Run 2 Run 3 Run 4 Run 5
Node.js 117 ms 109 ms 143 ms 114 ms 139 ms
Bun 70 ms 72 ms 95 ms 99 ms 80 ms

Comparing medians, Node.js took 117 ms and Bun took 80 ms, so Bun was 37 ms shorter. Both are under 0.15 seconds, a difference you would hardly notice for a server that stays running. It adds up when you restart the app many times during development or start a server for each test run.

Point

If you want to get faster without changing the code, running an Express app with bun app.ts alone handled more than 4 times as many requests per second in this test. If you need even more speed, rewriting the app with Bun.serve is an option. When you move an Express app as it is, it is worth checking before you move it whether the differences listed on the Bun official Node.js compatibility page, such as http.Server not inheriting from net.Server, affect your app.

​Posted by 86