Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akinjide
Created August 23, 2018 14:03
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 akinjide/1eed9fa076c4fe38b49289e4beeb69bf to your computer and use it in GitHub Desktop.
Save akinjide/1eed9fa076c4fe38b49289e4beeb69bf to your computer and use it in GitHub Desktop.
let cache = {}
const complex_computation = (a, b, timeout=5000, callback) => {
setTimeout(() => {
callback(null, a + b)
}, timeout)
}
const cached_computation = (a, b, callback) => {
const key = a + ':' + b
if (cache[key]) {
return callback(null, cache[key])
}
complex_computation(a, b, 10000, (err, data) => {
cache[key] = data
callback(err, data)
})
}
// EXEC
console.time("QueryTime")
cached_computation(2, 3, (err, data) => {
if (err != null) {
console.log(error)
return console.timeEnd("QueryTime")
}
console.log(data)
console.timeEnd("QueryTime")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment