map-memo
Generic memoization using Map
and WeakMap
.
Installing
npm install --save map-memo
What/Why?
Memoization in JavaScript has typically been limited to arguments with primitive values, or by utilizing hacks such as stringifying objects.
By storing arguments using a series of nested cache objects backed by Map
and WeakMap
, map-memo
is able to memoize functions with any
argument types.
Example
'use strict'; const memoize = ; { let v; for let i = 0; i < n; ++i v = ; return v;} let mem = ; console; // slowconsole; // fast!
Example with expire time in milliseconds
'use strict'; const memoize = ; { return Math;} let mem = ;console;console; // Same value as above ;
Example with asynchronous function
'use strict'; const memoize = ; { return { let v; for let i = 0; i < n; ++i v = ; ; };} let mem = ; ;