Last active 3 months ago

Avoiding Nested Loops with `new Map()`

gaiety's Avatar gaiety revised this gist 3 months ago. Go to revision

No changes

gaiety's Avatar gaiety revised this gist 3 months ago. Go to revision

No changes

gaiety revised this gist 3 months ago. Go to revision

1 file changed, 10 insertions

avoidingLoopsWithMap.js(file created)

@@ -0,0 +1,10 @@
1 + let myHugeArray = [...Array(10000).keys()].map(id => { return {id}; });
2 + let listIWant = [1010, 2020, 3030, 4040, 5050, 6060, 7070, 8080, 9090];
3 +
4 + // INSTEAD OF:
5 + myHugeArray.filter(obj => listIWant.includes(obj.id));
6 +
7 + // TRY:
8 + let mapIWant = new Map();
9 + listIWant.forEach(id => mapIWant.set(id));
10 + myHugeArray.filter(obj => mapIWant.has(obj.id));
Newer Older