Skip to content

基准测试

基准测试只是基准测试,但对我们而言十分重要。

路由器

我们测量了多款 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.0Requests/sec: 136112
Fast4.0.0-beta.1Requests/sec: 103214
Megalo0.3.0Requests/sec: 64597
Faster5.7Requests/sec: 54801

Released under the MIT License.