npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pwp-app/koa-rapid-router

v1.1.0

Published

A fixed version of koa-rapid-router

Downloads

5

Readme

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

| command | architecture | Latency | Req/Sec | Bytes/Sec | | :-----: | :----------: | :-----: | :-----: | :-------: | | test:koa | koa + koa-router | 245.07 ms | 394.25 | 56 kB | | test:fast | fastify | 1.96 ms | 49324 | 7 MB | | test:rapid | koa + koa-rapid-router | 2.17 ms | 44828.8 | 6.37 MB | | test:http | http + koa-rapid-router | 1.64 ms | 58911.2 | 5.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

| command | architecture | Latency | Req/Sec | Bytes/Sec | | :-----: | :----------: | :-----: | :-----: | :-------: | | test:koa | koa + koa-router | 220.29 ms | 441.75 | 62.7 kB | | test:fast | fastify | 1.9 ms | 50988.65 | 7.24 MB | | test:rapid | koa + koa-rapid-router | 2.32 ms | 41961.6 | 5.96 MB | | test:http | http + koa-rapid-router | 1.82 ms | 53160.8 | 5.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