1.0.0 • Published 4 years ago

breeze-web-router v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

koa-rapid-router

It is a routing architecture suitable for any service, and we usually use it on KOA, this is currently the fastest routing architecture.

Install

npm i koa-rapid-router

Document

Get started

Usage in koa

const Koa = require('koa');
const Router = require('koa-rapid-router');
const app = new Koa();
const route = new Router();
const router = route.create('/interface/api');
router.get('/uuid/{uid:number}', async (ctx) => {
  ctx.body = ctx.params.uid;
});
app.use(route.Koa()).listen(3000, err => {
  if (err) throw err;
  console.log('app run at 3000');
});

Add your own types

route.expression('xyz', '[x-z]+');
// then you can use it like this
router.get('/uuid/{uid:xyz}', async (ctx) => {
  ctx.body = ctx.params.uid;
});

Performance

Its performance is 100 times faster then koa-router, but it's similar to fastify (if you don't use the KOA infrastructure, use http). Test sample: 10,000 static routes are injected into different architectures. The test commands are the same: autocannon -c 100 -d 40 -p 10 <url>

Using static Router

for (let i = 0; i < 10000; i++) {
  router.get('/uuid/' + (i + 1), async (ctx) => ctx.body = 'ok');
  vrouter.get('/uuid/' + (i + 1), (res) => res.end('ok'));
  route_2.get('/interface/api/uuid/' + (i + 1), async (ctx) => ctx.body = 'ok');
  fastify.get('/interface/api/uuid/' + (i + 1), (request, reply) => reply.send('ok'));
}

Preview:

koa-rapid-router:static

Results

commandarchitectureLatencyReq/SecBytes/Sec
test:koakoa + koa-router245.07 ms394.2556 kB
test:fastfastify1.96 ms493247 MB
test:rapidkoa + koa-rapid-router2.17 ms44828.86.37 MB
test:httphttp + koa-rapid-router1.64 ms58911.25.95 MB

Using Dynamic Router

router.get('/zzz/{a:number}', async (ctx) => ctx.body = 'ok');
vrouter.get('/zzz/{a:number}', (res) => res.end('ok'));
route_2.get('/interface/api/zzz/:a(\\d+)', async (ctx) => ctx.body = 'ok');
fastify.get('/interface/api/zzz/:a', (request, reply) => reply.send('ok'));

Preview:

koa-rapid-router:dynamic

Results

commandarchitectureLatencyReq/SecBytes/Sec
test:koakoa + koa-router220.29 ms441.7562.7 kB
test:fastfastify1.9 ms50988.657.24 MB
test:rapidkoa + koa-rapid-router2.32 ms41961.65.96 MB
test:httphttp + koa-rapid-router1.82 ms53160.85.37 MB

Result

It is clear from the data that the performance advantages of the service can be established through http + koa-rapid-router. And fastify, the fastest route, has been completely defeated by rapid-router.

Data tells us that KOA architecture performance is very poor, we are fully possible to achieve near native HTTP performance routing, as long as we continue to explore the factors affecting performance, we will be able to close to native performance. It's the same as the principle of approaching the speed of light. It's impossible to be equal, but it's approachable.

How to test?

First open a new command line:

npm run dev

Then, open a new command line

npm run test

You can see a very shocking result.

License

MIT

Copyright (c) 2018-present, yunjie (Evio) shen