function memoization(key) { key = key || ''; return memoization.cache[key] = typeof memoization.cache[key] === 'undefined' ? getRandom(256) : memoization.cache[key]; } memoization.cache = {}; function getRandom(max){ return Math.floor(Math.random() * max); } console.log(memoization('test')); console.log(memoization('test')); console.log(memoization('test1'));