lukeed / trouter

:fish: A fast, small-but-mighty, familiar fish...errr, router*
MIT License
634 stars 23 forks source link

perf: methods comparison #21

Closed Kikobeats closed 1 year ago

Kikobeats commented 1 year ago

It's saving ~10ms

closes #19

Kikobeats commented 1 year ago

done; next PR will be to move from Travis to GitHub Actions 😛

lukeed commented 1 year ago

Quick bench on Node 18.12.1 because I didn't know what ~10ms meant in context.

# Before
GET / 
  12,864,415 ops/sec ±0.79% (95 runs sampled)
POST /users 
  15,691,754 ops/sec ±0.24% (96 runs sampled)
GET /users/:id 
   9,154,394 ops/sec ±0.15% (98 runs sampled)
PUT /users/:id/books/:title? 
   8,657,366 ops/sec ±0.16% (101 runs sampled)
DELETE /users/:id/books/:title 
   7,996,343 ops/sec ±0.21% (99 runs sampled)
PATCH /^[/]users[/](?<id>\d+)[/]?$/ 
   6,144,565 ops/sec ±0.13% (101 runs sampled)
OPTIONS /^[/]users[/](?<id>\d+)[/]books[/](?<title>[^/]+)[/]?$/ 
   4,634,106 ops/sec ±0.15% (96 runs sampled)
HEAD /hello (all) 
  13,135,000 ops/sec ±0.17% (100 runs sampled)

# After
GET / 
  14,366,336 ops/sec ±0.27% (95 runs sampled)
POST /users 
  16,363,969 ops/sec ±0.13% (98 runs sampled)
GET /users/:id
   9,146,330 ops/sec ±0.18% (95 runs sampled)
PUT /users/:id/books/:title? 
   8,896,106 ops/sec ±0.15% (100 runs sampled)
DELETE /users/:id/books/:title 
   8,133,663 ops/sec ±0.24% (97 runs sampled)
PATCH /^[/]users[/](?<id>\d+)[/]?$/ 
   6,202,659 ops/sec ±0.13% (99 runs sampled)
OPTIONS /^[/]users[/](?<id>\d+)[/]books[/](?<title>[^/]+)[/]?$/ 
   4,715,342 ops/sec ±0.18% (99 runs sampled)
HEAD /hello (all) 
  13,072,490 ops/sec ±0.13% (100 runs sampled)

For static matching (first two & last one) there is 4-10% perf increase, if any. Everything else is limited by RegExp matching

Thanks for picking this up~