https://github.com/starryinternet/map-memo
Generic memoization with Map and WeakMap
https://github.com/starryinternet/map-memo
javascript memoization memoize weakmap
Last synced: 5 days ago
JSON representation
Generic memoization with Map and WeakMap
- Host: GitHub
- URL: https://github.com/starryinternet/map-memo
- Owner: StarryInternet
- License: mit
- Created: 2016-05-29T20:58:17.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2020-02-12T15:40:02.000Z (about 6 years ago)
- Last Synced: 2024-04-24T20:26:20.696Z (almost 2 years ago)
- Topics: javascript, memoization, memoize, weakmap
- Language: JavaScript
- Size: 11.7 KB
- Stars: 10
- Watchers: 6
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: License.md
Awesome Lists containing this project
README
# map-memo
[](https://travis-ci.org/StarryInternet/map-memo)
Generic memoization using `Map` and `WeakMap`.
---
### Installing
```
npm install --save @starryinternet/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
```js
'use strict';
const memoize = require('@starryinternet/map-memo');
function loop( fn, n ) {
let v;
for ( let i = 0; i < n; ++i ) {
v = fn( i );
}
return v;
}
let mem = memoize( loop );
console.log( mem( Math.sqrt, 1e9 ) ); // slow
console.log( mem( Math.sqrt, 1e9 ) ); // fast!
```
### Example with expire time in milliseconds
```js
'use strict';
const memoize = require('@starryinternet/map-memo');
function getRandom() {
return Math.random();
}
let mem = memoize( getRandom, { ttl: 1000 } );
console.log( mem() );
console.log( mem() ); // Same value as above
setTimeout( function() {
console.log( mem() ); // Different value
}, 1001 );
```
### Example with asynchronous function
```js
'use strict';
const memoize = require('@starryinternet/map-memo');
function loopAsync( fn, n ) {
return new Promise( ( resolve, reject ) => {
let v;
for ( let i = 0; i < n; ++i ) {
v = fn( i );
}
resolve( v );
});
}
let mem = memoize( loopAsync );
mem( Math.sqrt, 1e9 ).then( result => {
console.log( result ); // slow
mem( Math.sqrt, 1e9 ).then( console.log ); // fast!
});
```