Published
Edited
Mar 31, 2022
1 star
Insert cell
Insert cell
memoize = (fn) => {
const CACHE = {};
return (...args) => {
const cacheKey = JSON.stringify(args);
// Check if result is already cached
if (CACHE[cacheKey] !== undefined) {
return CACHE[cacheKey];
}
// If not, compute the result
const result = fn(...args);
// Cache the result and return
CACHE[cacheKey] = result;
return result;
};
}
Insert cell
Insert cell
function sleep1Sec(returnValue) {
return new Promise((resolve) => {
setTimeout(() => resolve(returnValue), 1000);
});
}
Insert cell
sleep1Sec(3)
Insert cell
memoizeSleep1Sec = memoize(sleep1Sec)
Insert cell
memoizeSleep1Sec(2)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more