Provided you don't need to worry about keeping track of calculated nulls, you can make use of null-ish coalescing assignment (??=
).
function memoize(result) {
const cache = {};
return function() {
cache[result] ??= calculate(result);
return cache[result];
};
}