There is a lot of value when using a Set over an Array.
I learned yesterday that Set#has(item) performs an O(log n) algorithm to determine if the value exists in it. I was able to get a small speed increase in my javascript for it.
It's entirely possible I learned wrong! I took the statement at face value, knowing I couldn't verify it myself. If that's the case, then I'm really happy I use Set all the time now.
Is it correct to say that Set is like array, but with unique items only and Map is like object where keys might be of any type (not limited to strings and symbols)?
Well Set might look like an array but there are some notable differences. Apart from unique values, sets have different ways for initializing, accessing / adding / removing values.
Correct. Iteration is possible in the insertion order, but items are not indexed and they cannot be sorted unless converted to array beforehand. Maybe worth pointing out.
Agree. Sets also don't have array methods like map/filter/reduce and others. I just don't use Set/Map in my daily work and always forget what data structure is used for. By comparing Set to Array and Map to Object I want to create a mental connection in my head to remember the purpose of these data structures without looking in documentation.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
There is a lot of value when using a
Setover anArray.I learned yesterday that
Set#has(item)performs anO(log n)algorithm to determine if the value exists in it. I was able to get a small speed increase in my javascript for it.Good article!
AFAIK,
Set.hasimplementation in V8 is really fast - it hasO(1)time complexity.It's entirely possible I learned wrong! I took the statement at face value, knowing I couldn't verify it myself. If that's the case, then I'm really happy I use Set all the time now.
Is it correct to say that
Setis like array, but with unique items only andMapis like object where keys might be of any type (not limited to strings and symbols)?Well
Setmight look like an array but there are some notable differences. Apart from unique values, sets have different ways for initializing, accessing / adding / removing values.Sets are also unordered lists of elements, unlike arrays.
Why? We can use forEach method on Sets to iterate the collection by insertion order.
Correct. Iteration is possible in the insertion order, but items are not indexed and they cannot be sorted unless converted to array beforehand. Maybe worth pointing out.
Agree. Sets also don't have array methods like map/filter/reduce and others. I just don't use Set/Map in my daily work and always forget what data structure is used for. By comparing Set to Array and Map to Object I want to create a mental connection in my head to remember the purpose of these data structures without looking in documentation.
Are set values unique, I can't remember, but if they are passing a string would be destructive, hello world would be helo wrd
Yes, Set values are unique. It will filter-out the duplicate characters if you pass a string to create a set.
So that's good for something I'm sure 😋
Man, love those Emojis 👌👀
🙏