once
There was a time, before Nodejs 8, when we were so beaten to death by JS callbacks that it was not easy to say love. And the emergence of Koa, especially the CO module, let us flock to. Compact code style, efficient performance, and a new syntax that allows you to use JS. Are the broad JSer yearning. Koa explains Expressive middleware for Node.js using ES2017 Async Functions.
now
With Nodejs 10, we no longer need to use [– Harmony], and one of Koa’s strengths is no longer an advantage.
Performance?
With the help of V8 6.0+, Express performance is starting to match Koa performance. Here’s the test code:
// version 2.11.0 const Koa = require(' Koa '); const app = new Koa(); app.use(ctx => { ctx.body = 'Hello Koa'; }); app.listen(3000);Copy the code
// version 4.17.1
const express = require('express');
const app = express();
app.use((req, res) => {
res.end('hello express');
})
app.listen(3000);
Copy the code
See the following data (MacOS10.12.6, Nodejs v10.17.0) :
The framework | Number of requests | concurrency | QPS Requests per second | Average TPR time |
---|---|---|---|---|
Koa | 5000 | 100 | 5500-6400. | 14-18ms |
Koa | 5000 | 200 | 6500-7700. | 26-35ms |
express | 5000 | 100 | 5300-6100. | 16-20ms |
express | 5000 | 200 | 5700-7600. | 24-32ms |
Because my computer is rubbish, it only supports 200 complications at most, if more than AB, it will die. The above data are the average data obtained through multiple ab compression.
ab -n 5000 -c 200 http://localhost:3000/
Copy the code
Koa is no different from other frameworks in terms of future-oriented syntactic compatibility due to Nodejs evolution.
Once we chose Koa in order to pursue a little bit of extreme performance, especially under high concurrency, if it is 1000W concurrency, a request is so fast ten milliseconds, so long can save tens of thousands of seconds of total processing time, can process hundreds of thousands of requests, but now it is unnecessary.
New features of JS language?
Once upon a time we could challenge Koa to experience the updated JS syntax, and challenge Koa to solve the callback hell, and grind ahead in Koa’s middleware ecosystem with no official support. Also added a lot of fun for everyone. Pain and pleasure. But Nodejs itself now supports more than 99% of the new syntax.
Minimalist frames?
This is a bogus proposition. I worked with a member of the Nodejs Core team (TSC) when I was at IBM. I had the pleasure of discussing KOA and Express with him, and he asked me, “Wouldn’t you need Router to write Web services with Nodejs? Will you not need the BodyParse? Is Cookie parsing not required? No, it won’t. So you take a very simple framework, and then you have to install them back to look like Express. So what problem does he solve with minimalism? “And enterprise applications don’t care about the tens of milliseconds of performance.
after
Is Koa still our first choice? He was never the best choice for me. I am a loyal user of Express. In fact, I mainly benefited from my early contact with Express from its maintenance team. Moreover, most of my business scenarios are enterprise-level applications, and stability and maturity are the first factors.
But Koa brings me a lot of happiness that Express cannot. New horizons and fun. Because Koa lacks support for a lot of middleware, most of the middleware has to be written. During this period of personal growth has been very great. But not all teams can handle it. I’ve seen some teams get slowed down by Koa and even end up capsizing. In this respect, Egg does solve a lot of problems for technical teams who are relatively weak and want to try the Nodejs technology stack.