Skip to content

Instantly share code, notes, and snippets.

@hergaiety
Created May 16, 2019 14:46
Show Gist options
  • Save hergaiety/ffdd1e0070f527bb53f00d813c92278f to your computer and use it in GitHub Desktop.
Save hergaiety/ffdd1e0070f527bb53f00d813c92278f to your computer and use it in GitHub Desktop.
Avoiding Nested Loops with `new Map()`
let myHugeArray = [...Array(10000).keys()].map(id => { return {id}; });
let listIWant = [1010, 2020, 3030, 4040, 5050, 6060, 7070, 8080, 9090];
// INSTEAD OF:
myHugeArray.filter(obj => listIWant.includes(obj.id));
// TRY:
let mapIWant = new Map();
listIWant.forEach(id => mapIWant.set(id));
myHugeArray.filter(obj => mapIWant.has(obj.id));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment