性能基准

性能基准只是基准,但对我们来说非常重要。


路由器基准测试

我们测试了多个 JavaScript 路由器的速度。例如,find-my-way 是 Fastify 内部使用的非常快的路由器。

测试对象包括:

  • @medley/router
  • find-my-way
  • koa-tree-router
  • trek-router
  • express(包括请求处理)
  • koa-router

首先,我们向每个路由器注册以下路由(接近真实项目中的场景):

  1. export const routes: Route[] = [
  2. { method: 'GET', path: '/user' },
  3. { method: 'GET', path: '/user/comments' },
  4. { method: 'GET', path: '/user/avatar' },
  5. { method: 'GET', path: '/user/lookup/username/:username' },
  6. { method: 'GET', path: '/user/lookup/email/:address' },
  7. { method: 'GET', path: '/event/:id' },
  8. { method: 'GET', path: '/event/:id/comments' },
  9. { method: 'POST', path: '/event/:id/comment' },
  10. { method: 'GET', path: '/map/:location/events' },
  11. { method: 'GET', path: '/status' },
  12. { method: 'GET', path: '/very/deeply/nested/route/hello/there' },
  13. { method: 'GET', path: '/static/*' },
  14. ]

然后,我们向这些路由发送请求,例如:

  1. const routes: (Route & { name: string })[] = [
  2. { name: 'short static', method: 'GET', path: '/user' },
  3. { name: 'static with same radix', method: 'GET', path: '/user/comments' },
  4. { name: 'dynamic route', method: 'GET', path: '/user/lookup/username/hey' },
  5. { name: 'mixed static dynamic', method: 'GET', path: '/event/abcd1234/comments' },
  6. { name: 'post', method: 'POST', path: '/event/abcd1234/comment' },
  7. { name: 'long static', method: 'GET', path: '/very/deeply/nested/route/hello/there' },
  8. { name: 'wildcard', method: 'GET', path: '/static/index.html' },
  9. ]

Node.js 下的测试结果

以下是 Node.js 上的性能测试截图:

性能基准 - 图1 性能基准 - 图2 性能基准 - 图3 性能基准 - 图4 性能基准 - 图5 性能基准 - 图6 性能基准 - 图7 性能基准 - 图8


Bun 下的测试结果

以下是 Bun 上的性能测试截图:

性能基准 - 图9 性能基准 - 图10 性能基准 - 图11 性能基准 - 图12 性能基准 - 图13 性能基准 - 图14 性能基准 - 图15 性能基准 - 图16


Cloudflare Workers 性能

在 Cloudflare Workers 的路由器性能测试中,Hono 是最快的

  1. Hono x 402,820 ops/sec ±4.78% (80 runs sampled)
  2. itty-router x 212,598 ops/sec ±3.11% (87 runs sampled)
  3. sunder x 297,036 ops/sec ±4.76% (77 runs sampled)
  4. worktop x 197,345 ops/sec ±2.40% (88 runs sampled)
  5. Fastest is Hono
  6. Done in 28.06s.

Deno 性能

在 Deno 上,Hono 相比其他框架依然是最快的:

  • 测试环境:Apple MacBook Pro, 32 GiB, M1 Pro, Deno v1.22.0
  • 测试脚本benchmarks/deno
  • 测试方法

    1. bombardier --fasthttp -d 10s -c 100 'http://localhost:8000/user/lookup/username/foo'
框架 版本 Requests/sec
Hono 3.0.0 136,112
Fast 4.0.0-beta.1 103,214
Megalo 0.3.0 64,597
Faster 5.7 54,801
oak 10.5.1 43,326
opine 2.2.0 30,700

更多基准测试结果:denosaurs/bench