function  add ( a:  number ,  b:  string )  { return  a +  b
} 
npm  install  --save-dev typescript @types/node ts-node tsconfig.json
interface  User  { id:  number name:  string email:  string 
} function  createUser ( user:  User)  { 
} 
维度 Express Fastify 性能 每秒处理约5000请求 每秒处理约15000请求 代码侵入性 需要中间件链 单文件配置 社区支持 90万+ GitHub Star 40万+ GitHub Star TypeScript支持 完整但需手动配置 内置TypeScript模板 
 
                                                 
           
           
           
              基准测试环境 
             16核CPU/32GB内存 
             1000并发请求 
             Express: 2.1s响应延迟 
             Fastify: 0.7s响应延迟 
                 
  
{ "compilerOptions" :  { "target" :  "ES2020" , "module" :  "commonjs" , "strict" :  true , "outDir" :  "./dist" } , "include" :  [ "src/**/*" ] 
} 
import  express from  'express' ; 
import  {  User }  from  './models/user' ; const  app =  express ( ) ; 
app. get ( '/users' ,  ( req:  Request,  res:  Response)  =>  { res. json ( {  users:  User. find ( )  } ) ; 
} ) ; app. listen ( 3000 ) ; 
{ "compilerOptions" :  { "target" :  "ES2020" , "module" :  "commonjs" , "strict" :  true , "types" :  [ "node" ,  "fastify" ] } 
} 
import  fastify from  'fastify' ; const  app =  fastify ( ) ; app. get < { Querystring:  {  id:  number  } 
} > ( '/users' ,  async  ( req,  res)  =>  { const  user =  await  User. findById ( req. query. id) ; return  user; 
} ) ; app. listen ( {  port:  3000  } ) ; 
app. get ( '/api/users' ,  ( req,  res)  =>  { res. send ( users) ; 
} ) ; 
@Route ( '/api/users' ) 
export  class  UserController  { @Get ( ) async  getAll ( )  { return  User. find ( ) ; } 
} 
app. use ( bodyParser. json ( ) ) ; 
app. use ( cors ( ) ) ; 
app. use ( mongoSanitize ( ) ) ; 
app. use ( helmet ( ) ) ; 
fastify. register ( require ( '@fastify/cors' ) ,  { origin:  true 
} ) ; 
import  {  NestFactory }  from  '@nestjs/core' ; 
import  {  AppModule }  from  './app.module' ; async  function  bootstrap ( )  { const  app =  await  NestFactory. create ( AppModule) ; await  app. listen ( 3000 ) ; 
} 
bootstrap ( ) ; 
const  server =  fastify ( ) ; server. get ( '/realtime' ,  async  ( request,  reply )  =>  { const  data =  await  realTimeDataProcessor ( ) ; reply. header ( 'Content-Type' ,  'text/event-stream' ) . send ( data) ; 
} ) ; 
node --expose-gc build/app.js
// 使用128MB内存
node --expose-gc build/app.js
// 使用64MB内存
artillery run config.yml
config : target :  "http://localhost:3000" 
scenarios : -  flow : -  get : url :  "/api/users" duration :  60 arrivalRate :  100 
const  arr =  [ 1 ,  2 ,  3 ] ; 
arr. push ( "test" ) ;  
const  arr:  number [ ]  =  [ 1 ,  2 ,  3 ] ; 
npm  install  @fastify/express
import  fastify from  'fastify' ; 
import  {  json }  from  'express' ; const  app =  fastify ( ) ; 
app. register ( require ( '@fastify/express' ) ) ; 
app. use ( json ( ) ) ; 
                                                 小规模  
           中大型  
           
           
              项目规模 
             选择Fastify 
             选择Express/NestJS 
             关注性能优化 
             需要复杂模块化 
                 
  
npm  run build  
npm  run watch   
npm  run test