Skip to content

memoize

This is a memoize function, which is used to cache the return values of a function. When called again with the same parameters, it will return the cached result, avoiding redundant calculations.

By default, uses JSON.stringify(arguments) as the cache key.

Added in v0.0.1

Usage

typescript
import { memoize } from 'parsnip-kit'

function expensiveCalculation(a: number, b: number): number {
  // Simulate an expensive computation process
  console.log('Calculating...')
  return a + b
}

const memoizedCalc = memoize(expensiveCalculation)
memoizedCalc(2, 3) // Calculating...,returns 5
memoizedCalc(2, 3) // directly return 5, do not call expensiveCalculation

// Use a custom resolver
const memoizedCalcWithResolver = memoize(
  expensiveCalculation,
  (a, b) => a + b
)
memoizedCalcWithResolver(2, 3) // Calculating...,returns 5
memoizedCalcWithResolver(4, 1) // directly return 5, do not call expensiveCalculation

const memoizedCalcWithAllArgs = memoize(
  expensiveCalculation,
  (a, b) => a + b,
  [2, 3]
)
// Calculating..., call expensiveCalculation to cache
memoizedCalcWithAllArgs(2, 3)
// directly return 5, do not call expensiveCalculation

API

Type Parameter

ArgTypeDescription
Textends (...args: any[]) => anyType of function to memoize

Arguments

ArgTypeOptionalDefaultDescription
funcTfalseundefinedThe function to memoize
resolver( ...args: Parameters<T> ) => anytrueundefinedA function used to generate cache keys
initCache Parameters<T>trueundefinedParameters for initializing the cache

Returns

Type
( ...args: Parameters<T> ) => ReturnType<T>