Memoization implementation in Javascript