1
0
Fork 0

Update Posts “using-js-sets-to-find-elements”

This commit is contained in:
Jo Wroten 2019-07-09 20:34:40 +00:00
parent dacade6fcc
commit caad8f6ea9

View file

@ -5,26 +5,7 @@ tags:
- Tech - Tech
description: >- description: >-
JS's `new Set()` can be the new Hash table optimization for reducing nested JS's `new Set()` can be the new Hash table optimization for reducing nested
loops. loops. So I've put together a little code snippet to show this in practice.
```js
// Example Data
let hugeArray = [...Array(10000).keys()].map(id => { return {id}; }); // [{id:
0}, ..., {id: 9999}]
let needles = [1010, 2020, 3030, 4040, 5050, 6060, 7070, 8080, 9090];
// Finding matching elements
let needlesSet = new Set(needles);
let matches = hugeArray.filter(obj => needlesSet.has(obj.id));
```
--- ---
Functionally for one check, this is identical to checking the `.indexOf()` or `.includes()` of an Array. However, in cases where you're filtering a large array down to match a list of ID's, for example, the nested looping can result in a performance hit. Functionally for one check, this is identical to checking the `.indexOf()` or `.includes()` of an Array. However, in cases where you're filtering a large array down to match a list of ID's, for example, the nested looping can result in a performance hit.