Last active
October 1, 2019 19:31
-
-
Save alexreardon/17ce55a4cc635d725f85f8ad81952318 to your computer and use it in GitHub Desktop.
Lazy lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getIdMap = memoizeOne((array) => { | |
return array.reduce((previous, current) => { | |
previous[current.id] = array[current]; | |
return previous; | |
}, {}); | |
}); | |
const foo = { id: 'foo' }; | |
const bar = { id: 'bar' }; | |
// our lovely ordered structure | |
const ordered = [ foo, bar ]; | |
// lazily computed map for fast lookups | |
const map1 = getMap(ordered); | |
map1['foo'] === foo; // true | |
map1['bar'] === bar; // true | |
map1['baz'] === undefined; // true | |
const map2 = getMap(ordered); | |
// returned the same map as before - no recomputation required | |
const map1 === map2; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also: