Gaiety's visual representation as a fursona (an original furry fandom character) depicted as an animated gif falling downward with a controlled hand outstretched. Faer hair and tail waves in the wind while their floppy dog ears flop about as well. Fae are an anthromorphic canine like a german shepherd clothed like a human in a t-shirt and denim shorts. Art is by Lynte.

Using JS Set's to Find Elements in Posts

JS's new Set() can be the new Hash table optimization for reducing nested loops. So I've put together a little code snippet to show this in practice.

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.

Traditionally hash tables were used for speeding up this search, but with ES2015 we can now leverage Sets which are easier to read and still result in a single loop that's equal to the length of the Array and no more.

(Can be further optimized by ending when running out of needles)

// 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));
js

https://gitlab.com/snippets/1873655

ava@wroten.me

Wish to contact me about meetup or conference talks, work opportunities or otherwise? Reach out to me via email.

Resume

Work history, skills, volunteering and talks given all in a one page PDF.

Blog

Programming, art, design, and other queer things.

Git

Coding projects and such on a self hosted Forgejo instance.