Last active
January 11, 2022 09:26
-
-
Save adrai/39fa41bda8249f0645c6087efdc5c789 to your computer and use it in GitHub Desktop.
aws-lambda-fastify (ESM optimization)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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