Skip to content

Instantly share code, notes, and snippets.

@adrai
Last active January 11, 2022 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrai/39fa41bda8249f0645c6087efdc5c789 to your computer and use it in GitHub Desktop.
Save adrai/39fa41bda8249f0645c6087efdc5c789 to your computer and use it in GitHub Desktop.
aws-lambda-fastify (ESM optimization)
// import { handler } from './lambda.js' // let's import it dynamically, so we can measure...
const evt = {
httpMethod: 'GET',
path: '/test',
headers: {}
}
const ctx = {}
let st = Date.now()
let { handler } = await import('./lambda.js')
console.log(`import 1) ${Date.now() - st}ms`)
// without .ready() call => import 1) 205ms
// with .ready() call => import 1) 1087ms
st = Date.now()
await handler(evt, ctx)
console.log(`handler 1) ${Date.now() - st}ms`)
// without .ready() call => handler 1) 910ms
// with .ready() call => handler 1) 57ms
st = Date.now()
await import('./lambda.js')
console.log(`import 2) ${Date.now() - st}ms`)
// without .ready() call => import 2) 2ms
// with .ready() call => import 2) 2ms
st = Date.now()
await handler(evt, ctx)
console.log(`handler 2) ${Date.now() - st}ms`)
// without .ready() call => handler 2) 1ms
// with .ready() call => handler 2) 1ms
st = Date.now()
await handler(evt, ctx)
console.log(`handler 3) ${Date.now() - st}ms`)
// without .ready() call => handler 3) 0ms
// with .ready() call => handler 3) 0ms
import awsLambdaFastify from 'aws-lambda-fastify'
import app from './app.js'
export const handler = awsLambdaFastify(app)
// await app.ready() // needs to be placed after awsLambdaFastify call because of the decoration: https://github.com/fastify/aws-lambda-fastify/blob/master/index.js#L9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment