Skip to content

Instantly share code, notes, and snippets.

@inPhoenix
Created August 8, 2018 20:34
Show Gist options
  • Save inPhoenix/a109a630b34c975018773f479754a2f9 to your computer and use it in GitHub Desktop.
Save inPhoenix/a109a630b34c975018773f479754a2f9 to your computer and use it in GitHub Desktop.
cache array.find on long list example
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