跳转到正文

基准测试 ​

基准测试终究只是基准测试,但对我们很重要。

路由器 ​

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

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

首先,我们为每个路由器注册了以下路由。 它们与实际应用中使用的路由相似。

ts
export const 
routes
: Route[] = [
{
method
: 'GET',
path
: '/user' },
{
method
: 'GET',
path
: '/user/comments' },
{
method
: 'GET',
path
: '/user/avatar' },
{
method
: 'GET',
path
: '/user/lookup/username/:username' },
{
method
: 'GET',
path
: '/user/lookup/email/:address' },
{
method
: 'GET',
path
: '/event/:id' },
{
method
: 'GET',
path
: '/event/:id/comments' },
{
method
: 'POST',
path
: '/event/:id/comment' },
{
method
: 'GET',
path
: '/map/:location/events' },
{
method
: 'GET',
path
: '/status' },
{
method
: 'GET',
path
: '/very/deeply/nested/route/hello/there' },
{
method
: 'GET',
path
: '/static/*' },
]

然后,向以下端点发送请求。

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

下面来看测试结果。

在 Node.js 上 ​

以下截图展示了在 Node.js 上的测试结果。

在 Bun 上 ​

以下截图展示了在 Bun 上的测试结果。

Cloudflare Workers ​

与 Cloudflare Workers 上的其他路由器相比,Hono 的速度最快。

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

Deno ​

与 Deno 上的其他框架相比,Hono 的速度最快。

  • 测试设备: Apple MacBook Pro, 32 GiB, M1 Pro, Deno v1.22.0
  • 测试脚本: benchmarks/deno
  • 测试方法: bombardier --fasthttp -d 10s -c 100 'http://localhost:8000/user/lookup/username/foo'
框架版本结果
Hono3.0.0每秒请求数: 136112
Fast4.0.0-beta.1每秒请求数: 103214
Megalo0.3.0每秒请求数: 64597
Faster5.7每秒请求数: 54801
oak10.5.1每秒请求数: 43326
opine2.2.0每秒请求数: 30700

另一个基准测试结果:denosaurs/bench

Bun ​

Hono 是 Bun 上速度最快的框架之一。 可以查看下面的测试。

基于 MIT 许可证发布。