Skip to content

Instantly share code, notes, and snippets.

@dashmug dashmug/sample.js
Last active Jun 8, 2018

Embed
What would you like to do?
// Don't
function handler(event, context) {
const s3 = new AWS.S3()
return s3.upload({}).promise()
}
// Better - Init variables outside the handler
const s3 = new AWS.S3()
function handler(event, context) {
return s3.upload({}).promise()
}
// Example for in-memory caching
const cache = {}
function handler(event, context) {
const now = Date.now()
if ('userTypes' in cache && now < cache.userTypes.expiry) {
// Data is cached and still fresh
// One less database query.
return userTypes.data
}
// Reset cache with fresh data.
cache.userTypes = {
data: getUserTypesFromDatabase,
expiry: now + TWO_HOURS
}
return cache.userTypes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.