Created
August 8, 2018 20:34
-
-
Save inPhoenix/a109a630b34c975018773f479754a2f9 to your computer and use it in GitHub Desktop.
cache array.find on long list example
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 nodes = [ | |
{ id:"abc", content:"Lorem ipsum"}, | |
{ id:"def", content:"Dolor sit" }, | |
// ... | |
]; | |
const keyedNodes = {} | |
const getNode = id => { | |
if (!keyedNodes[id]) { | |
keyedNodes[id] = nodes.find(n => n.id === id); | |
} | |
return keyedNodes[id]; | |
} | |
console.log(getNode('abc')); | |
// => { id:"abc", content:"Lorem ipsum" } | |
console.log(getNode('abc')); | |
// This time we are coming from keyedNodes! | |
// => { id:"abc", content:"Lorem ipsum" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment