memoize-one
A memoization library that only caches the result of the most recent arguments. This a fork of Alex Reardon's fantastic memoize-one JavaScript library, but is written in TypeScript and utilizes decorators to better facilitate our Local by Flywheel coding efforts.
Rationale
Unlike other memoization libraries, memoize-one-ts
only remembers the latest arguments and result.
No need to worry about cache busting mechanisms such as maxAge
, maxSize
, exclusions
and so on which can be prone to memory leaks.
memoize-one-ts
simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result.
Usage
Standard usage
; ;; memoizedAdd1, 2; // 3 memoizedAdd1, 2; // 3// Add function is not executed: previous result is returned memoizedAdd2, 3; // 5// Add function is called to get new value memoizedAdd2, 3; // 5// Add function is not executed: previous result is returned memoizedAdd1, 2; // 3// Add function is called to get new value.// While this was previously cached,// it is not the latest so the cached result is lost
Installation
# yarn yarn add memoize-one-ts # npm npm install memoize-one-ts --save
Custom equality function
You can also pass in a custom function for checking the equality of two sets of arguments
;;
An equality function should return true
if the arguments are equal. If true
is returned then the wrapped function will not be called.
The default equality function is a shallow equal check of all arguments (each argument is compared with ===
). The default equality function also does not check anything if the length of the arguments changes. You are welcome to decide if you want to return false
if the length
of the arguments is not equal
newArgs.everyshallowEqualnewArg, lastArgs, ;
A custom equality function needs to compare Arrays
. The newArgs
array will be a new reference every time so a simple newArgs === lastArgs
will always return false
.
Equality functions are not called if the this
context of the function has changed (see below).
Here is an example that uses a lodash.isequal
deep equal equality check
lodash.isequal
correctly handles deep comparing two arrays
;; ; ;; ;; result1 === result2; // false - difference reference ;; result3 === result4; // true - arguments are deep equal
this
memoize-one-ts
correctly respects this
control
This library takes special care to maintain, and allow control over the the this
context for both the original function being memoized as well as the returned memoized function.
Both the original function and the memoized function's this
context respect all the this
controlling techniques:
- new bindings (
new
) - explicit binding (
call
,apply
,bind
); - implicit binding (call site:
obj.foo()
); - default binding (
window
orundefined
instrict mode
); - fat arrow binding (binding to lexical
this
) - ignored this (pass
null
asthis
to explicit binding)
this
is considered an argument change
Changes to Changes to the running context (this
) of a function can result in the function returning a different value even though its arguments have stayed the same:
;; getA.calltemp1; // 20getA.calltemp2; // 30
Therefore, in order to prevent against unexpected results, memoize-one-ts
takes into account the current execution context (this
) of the memoized function.
If this
is different to the previous invocation then it is considered a change in argument. further discussion.
Generally this will be of no impact if you are not explicity controlling the this
context of functions you want to memoize with explicit binding or implicit binding.
memoize-one-ts
will detect when you are manipulating this
and will then consider the this
context as an argument.
If this
changes, it will re-execute the original function even if the arguments have not changed.
throw
s
When your result function There is no caching when your result function throws
If your result function throw
s then the memoized function will also throw.
The throw will not break the memoized functions existing argument cache.
It means the memoized function will pretend like it was never called with arguments that made it throw
.
; ; ;// console.log => 'called';// result function not called console.logvalue1 === value2;// console.log => true try catch e try catch e console.logfirstError !== secondError; ;// result function not called as the original memoization cache has not been bustedconsole.logvalue1 === value3;// console.log => true