Skip to content

perf(route): 5% faster routing#244

Closed
nigrosimone wants to merge 16 commits into
dimdenGD:mainfrom
nigrosimone:perf-route
Closed

perf(route): 5% faster routing#244
nigrosimone wants to merge 16 commits into
dimdenGD:mainfrom
nigrosimone:perf-route

Conversation

@nigrosimone

@nigrosimone nigrosimone commented Aug 18, 2025

Copy link
Copy Markdown
Contributor

What's new

  • Avoid unnecessary allocations and function calls
  • Use a non-async next for most cases

Improvements

  • ~20% less memory usage
  • ~5% faster routing

Benchmark with 1000 middleware

benchmark\route.js

// run benchmark with: node benchmark\route.js --iterations=50000 --expose-gc

"use strict";

const { run, bench, group } = require("mitata");

const expressPr = require("../src/index");
const expressMaster = require("ultimate-express");

const appPr = expressPr();
const appMaster = expressMaster();

appPr.set("etag", false);
appPr.set("declarative responses", false);

appMaster.set("etag", false);
appMaster.set("declarative responses", false);

for (let index = 0; index < 1000; index++) {
  appPr.use(async(req, res, next) => next());
  appMaster.use(async(req, res, next) => next());
}

appPr.get("/", async (req, res) => res.send());
appMaster.get("/", async (req, res) => res.send());

appPr.listen(3001, async () => {
  appMaster.listen(3002, async () => {
    group("Express route benchmark", () => {
      bench("PR", () => fetch("http://localhost:3001/", { method: "HEAD", keepalive: true }));
      if (global.gc) global.gc();
      bench("MASTER", () => fetch("http://localhost:3002/", { method: "HEAD", keepalive: true }));
    });

    await run();

    process.exit(0);
  });
});
image

Pipeline benchmark

Side note: this benchmark has only 1 route, no benefit from my changes

Master

| Metric             | Ultimate Express   | Express         | Difference      |
|--------------------|--------------------|-----------------|-----------------|
| Requests/sec       | 73253.96           | 8004.12         | 9.15x faster    |
| Avg Latency        | 2.73            ms | 34.34        ms | 12.57x faster   |
| Transfer/sec       | 14.39           MB | 1.82         MB | 7.90x faster    |

PR

| Metric             | Ultimate Express   | Express         | Difference      |
|--------------------|--------------------|-----------------|-----------------|
| Requests/sec       | 73314.74           | 8019.90         | 9.14x faster    |
| Avg Latency        | 2.73            ms | 36.01        ms | 13.19x faster   |
| Transfer/sec       | 14.40           MB | 1.82         MB | 7.91x faster    |

@nigrosimone nigrosimone changed the title perf(route): try to optimize routing perf(route): 9-10% faster routing Aug 19, 2025
@nigrosimone
nigrosimone marked this pull request as draft August 20, 2025 12:44
@nigrosimone nigrosimone changed the title perf(route): 9-10% faster routing perf(route): 5% faster routing Aug 20, 2025
@nigrosimone
nigrosimone marked this pull request as ready for review August 20, 2025 12:58
@dimdenGD

Copy link
Copy Markdown
Owner

This does seems a little bit faster, but code quality becomes way worse, so I'm not sure if to merge or not... You should try improving the code somehow (more comments, less repetition, fix formatting)

@nigrosimone

Copy link
Copy Markdown
Contributor Author

No problem, as soon as I have some free time I fix it

@nigrosimone
nigrosimone marked this pull request as draft August 28, 2025 19:19
@nigrosimone

nigrosimone commented Aug 30, 2025

Copy link
Copy Markdown
Contributor Author

@dimdenGD I fixed the formatting so that only the logic changes are more obvious now.

Do you think this is OK?

Updated bench
image

@nigrosimone
nigrosimone marked this pull request as ready for review August 30, 2025 08:06
@dimdenGD

Copy link
Copy Markdown
Owner

Still weird format:
image

Also code repeating twice:
image
If possible without performance degradation, need to make a function

@nigrosimone

This comment was marked as outdated.

@dimdenGD

Copy link
Copy Markdown
Owner

What about declaring it with function instead

@nigrosimone

This comment was marked as outdated.

@nigrosimone

This comment was marked as outdated.

@dimdenGD

Copy link
Copy Markdown
Owner

You reformatted entire file so now its impossible to see changes

@nigrosimone

This comment was marked as outdated.

@nigrosimone

nigrosimone commented Aug 31, 2025

Copy link
Copy Markdown
Contributor Author

@dimdenGD no way (without loose performance!) to make it a class function :(

I just added a comment to remind you to keep duplicated code parts the same 00b3a18

feel free to close the pr if you don't like the changes

@nigrosimone
nigrosimone deleted the perf-route branch April 4, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants