const add = (a, b) => (a + b); const cachingFunc = (cacheObj = {}) => { const cache = cacheObj; return (a, b) => { let hash = `${a}:${b}`; if (!cache[hash]) { console.log('Computing result'); cache[hash] = add(a, b); } else { console.log('Skipping computation'); } return cache[hash]; } } console.log('Test without pre-existing cache'); const cachingAdd = cachingFunc(); cachingAdd(3, 4); cachingAdd(3, 5); cachingAdd(3, 4); const cachingAddv2 = cachingFunc({ '3:7': 10, '4:2': 6 }); console.log('Test with existing cache'); cachingAddv2(3, 7);