contrast

If nodeJS is used to build a Service, we prefer Express or KOA, whereas Fastify tells us this:

Framework Version Router? Requests/sec
hapi 18.1.0 29998
Express 4.16.4 38510
Restify 8.0.0 39331
Koa 2.7.0 50933
Fastify 2.0.0 76835
http.Server 10.15.2 71768

As you can see from the data, Koa’s performance is much better than Express’s. Of course, its tests are based on simple, single-route tests. However, we can see that Fastify’s performance is much better than Koa’s. Anyone who has used Fastify will be surprised at the speed of its performance. The requested URL quickly matches the Callback. How to do that, in theory, is very simple, is to find its shortest path to match. So generally can quickly match, are through the way of space for time to achieve the effect.

One other thing I want to tell you is that Fastify is not the fastest.

The protagonist

Today’s hero is koA-Rapid-Router. Why do we start with KOA? Because the purpose of this article is really to compare koA-Router, not Fastify. This routing architecture is also designed to approach Fastify’s performance when using KOA (and KOA has its own performance issues, as testing has not exceeded Fastify’s).

Next, we will throw out a series of test data to show how bad the PERFORMANCE of the KOA-Router is. We tested this principle separately

  1. Inject 10000 static routes to each schema and test the last one.
  2. Use the same test commandautocannon -c 100 -d 40 -p 10 <url>
  3. Compare the performance differences between static routes and dynamic routes

The test code is all here

Static Route Comparison

Let’s write the following code

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'));
}
Copy the code

Then test NPM run test and get data:

Preview:

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

Statistically, the KOA-Router performed extremely poorly at an average of 394.25 at 10,000 routes, meaning it could only handle 394.25 requests per second. While koA + KOA-Rapid-Router processed 44,828.8. Using the same KOA model, the gap is obvious. I have made an analysis, which is mainly caused by the large number of internal loops in koA-Router. In a 10000 request loop, it’s very inefficient. How do we achieve the performance of 44828.8, mainly we maintain a static route list in memory, so that the program can find the callback we need in the fastest speed.

As you can see with Fastify, KOA’s own performance is problematic.

The koA-Router has no advantage over the static route, so let’s compare the dynamic route.

Dynamic Route Comparison

Let’s write the following code

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'));
Copy the code

We added this code to the 10000 static routing code to correct the test path, and we got the following data:

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

The comparison of dynamic routing shows how bad koA-Router is to some extent. It is basically stable at around 400 QPS for both static and dynamic routes. While KOA + KOA-Rapid-Router declined slightly, Fastify remained stable as usual. However, from the HTTP + KOA-rapid-Router model, Rapid completely surpasses Fastify. The PERFORMANCE of the KOA + KOA-Rapid-Router is about 100 times that of the KOA + KOA-Router. Koa + KOA-Rapid-Router is the best choice if we want high concurrency but still use the KOA ecosystem. If we’re all about performance, not ecology, then Fastify is the best choice.

So why not use HTTP + KOa-rapid-router, a faster route than Fastify, one might ask? That’s because HTTP + KOA-Rapid-Router requires a separate ecosystem and cannot be used on a large scale for the time being. Maybe eventually we can use a new enterprise service architecture based on KOA-Rapid-Router. That’s what I’m thinking.

At the end

The performance of the wheel we built is impossible to beat the performance of the HTTP module, we can only approach it indefinitely. It’s like the speed of light, you can only approach, not equal. High-performance architecture is all about conceptual models, and math.

Project open source at github.com/cevio/koa-r… Interested partners pay attention to, thank you.