Skip to content

Instantly share code, notes, and snippets.

@dashmug
Last active June 8, 2018 03:20
Show Gist options
  • Save dashmug/14947d90b2c83e93e6853900cae57eac to your computer and use it in GitHub Desktop.
Save dashmug/14947d90b2c83e93e6853900cae57eac to your computer and use it in GitHub Desktop.
// 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