机器信息:4C32G
测试工具:wrk
Node: v14.17.0

express.js

  1. 'use strict'
  2. const express = require('express')
  3. const app = express()
  4. app.get('/', function (req, res) {
  5. res.json({ hello: 'world' })
  6. })
  7. app.listen(3000)

fastify.js

  1. 'use strict'
  2. const fastify = require('fastify')()
  3. fastify.get('/', function (req, reply) {
  4. reply.send({ hello: 'world' })
  5. })
  6. fastify.listen(3000)
  7. ~

测试结果

  1. # express.js
  2. Running 10s test @ http://127.0.0.1:3000
  3. 12 threads and 400 connections
  4. Thread Stats Avg Stdev Max +/- Stdev
  5. Latency 55.36ms 11.53ms 173.22ms 93.16%
  6. Req/Sec 602.58 113.03 830.00 84.97%
  7. 72034 requests in 10.10s, 17.31MB read
  8. Requests/sec: 7134.75
  9. Transfer/sec: 1.71MB
  10. # fastify.js
  11. Running 10s test @ http://127.0.0.1:3000
  12. 12 threads and 400 connections
  13. Thread Stats Avg Stdev Max +/- Stdev
  14. Latency 16.26ms 5.73ms 105.76ms 96.26%
  15. Req/Sec 2.08k 490.82 14.63k 94.92%
  16. 249114 requests in 10.09s, 44.43MB read
  17. Requests/sec: 24688.94
  18. Transfer/sec: 4.40MB

fastify是express的3.4倍, 所以对性能有所追求的话,最好用fastify。