memoLastCall
Creates a memoized version of a function that caches only its most recent call
Usage
Creates a memoized version of a function that caches only its most recent call.
When the function is called with the same arguments as the previous call, it returns the cached result instead of recalculating. This is useful for optimizing expensive calculations when only the latest result needs to be cached, making it more memory-efficient than traditional memoization.
import * as _ from 'radashi'
const expensiveCalculation = (x: number, y: number): number => { console.log('Calculating...') return x + y}
const memoizedCalc = _.memoLastCall(expensiveCalculation)
memoizedCalc(2, 3) // Logs 'Calculating...'memoizedCalc(2, 3) // Returns cached resultmemoizedCalc(3, 4) // Logs 'Calculating...'memoizedCalc(2, 3) // Logs 'Calculating...'
Premature Optimization
It’s bad practice to reach for this function if you haven’t profiled your application and confirmed that the calculation you intend to memoize is the bottleneck.
Beyond performance gains, this function may come in handy if your calculation produces an object and you want memoized calls to return the same object reference for equality checks.