instacart/Snacks

View on GitHub
src/utils/memoize.js

Summary

Maintainability
A
0 mins
Test Coverage
export default func => {
  const memoizedFunc = function(...args) {
    let result
    const key = JSON.stringify(args)
    if (memoizedFunc.cache[key] !== undefined) {
      result = memoizedFunc.cache[key]
    } else {
      result = func(...args)
      memoizedFunc.cache[key] = result
    }
    return result
  }
  memoizedFunc.cache = {}
  return memoizedFunc
}