Function-level caching in javascript using memoization