Created
August 23, 2018 14:03
-
-
Save akinjide/1eed9fa076c4fe38b49289e4beeb69bf to your computer and use it in GitHub Desktop.
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
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