跳至內容

效能基準

效能基準只是基準,但對我們來說很重要。

路由器

我們測量了一些 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 上的結果。

bench

bench

bench

bench

bench

bench

bench

bench

在 Bun 上

以下螢幕截圖顯示了在 Bun 上的結果。

bench

bench

bench

bench

bench

bench

bench

bench

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 'https://127.0.0.1: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 許可發布。