ES6 provides a native Set:
let s = new Set();
let a = {};
let b = {};
s.add(a);
console.log(s.has(a)); // true
console.log(s.has(b)); // false
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from Ry- on Stack Overflow Top answer 1 of 13
48
ES6 provides a native Set:
let s = new Set();
let a = {};
let b = {};
s.add(a);
console.log(s.has(a)); // true
console.log(s.has(b)); // false
Run code snippetEdit code snippet Hide Results Copy to answer Expand
2 of 13
26
Here's a mad suggestion ... key it on the result of JSON.stringify(object)
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - The Set object lets you store unique values of any type, whether primitive values or object references.
What the heck the "new Set()" is?
It’s kinda returns an object, but objects have a pair prop: 'val', but ‘Set’ has only one prop/val(?)… When I do console.log(new Set([1,2,3,4,4,5,5,5,6])) in my console I have this result Set(5) {1, 2, 3, 4, 5,6}. As I remember it’s one of the ES6 feature but I’m not sure. More on forum.freecodecamp.org
[AskJS] When do you use the Set object?
Remove duplicate entries More on reddit.com
Are JS maps and sets really that ...useless?
Your article does a good job of pointing out the cases where you want maps and sets. For maps, it lets you use non-strings as keys, and it remembers the insertion order. For sets, you also get the removal of duplicates. So if you need that stuff, maps and sets are good tools. If you don't need that stuff, then you don't. Objects still are the right choice for most cases, because most cases don't need those extra features. And with fewer features, objects can specialize and get better performance for some cases. More on reddit.com
I discovered Set exists for the first time today... Is it really faster?
I use sets, maps, and spread syntax all the time. I’ve never “done the math” to see the specs of the speed for sets. Maps have methods that are way faster when trying to search and filter things in big data sets. More on reddit.com
YouTube
youtube.com › watch
Sets in Javascript (Set Object) - YouTube
Similar to Arrays, a Set can be used to store a sequence of values in Javascript. However, a Set can only store unique values, meaning we cannot store duplic...
Published: December 24, 2017
Reddit
reddit.com › r/javascript › [askjs] when do you use the set object?
r/javascript on Reddit: [AskJS] When do you use the Set object?
April 20, 2020 -
The author of this awesome post deleted their description. Unfortunately, commenters were agreeing that they used Set in the same scenarios as OP. The problem here is OP removed the post, so I can't see what they are agreeing with.
So, when should a Set be used, in your opinion?
Top answer 1 of 4
5
Remove duplicate entries
2 of 4
4
Set is similar to Array. It's faster, but more limited. The main difference is Set does not allow duplicates. If I wanted to replicate Set with Arrays, it would look like this: const SetLike = { _array: [], has: function(value) { return this._array.includes(value); }, add: function(value) { if (!this.has(value)) [].push(value); }, delete: function(value) { let idx = this._array.indexOf(value); if (idx !== -1) { this._array.splice(idx, 1); } }, clear: function() { this._array.splice(0, this._array.length); }, keys: function() { return this._array[Symbol.iterator]; } }; You can use them when you don't care about the order of items and just want to see if something is included in a list. It's essentially for collections, or even tagging. It's worth nothing that Set works with primitives like numbers and strings whereas WeakSet does not. An Object in a Set will be held in memory, much like if it's in an Array. WeakSet can't be iterated, but will tell you if an Object exists inside it's set. But adding an Object to a WeakSet will still let it get garbage selected. This is good for stuff like HTMLElement, where you want the element to be destroyed by the DOM and have no references. You can use that for "tagging" . Instead of doing element.dataset.isDirty = 'true', you can do myWeakSetOfDirtyElements.add(element). Then you can check via myWeakSetOfDirtyElements.has(element). Essentially, tagging Objects with a boolean. You aren't holding it in an Array, meaning the DOM can delete and it disappears from your WeakSet (not that you'd notice). Map is similar to Set, but each element has an associated value. It's very similar to using an {} where you use keys and values, but it's overall faster. WeakMap has the same loose reference (allowing garbage collection). So you can get an expanded value instead rather than a simple boolean representation of being included or not. In my opinion, WeakSet and WeakMap are the where the true strengths lie. Set and Map can be replicated, more or less, with Array and Object, respectively.
TutorialsPoint
tutorialspoint.com › javascript › javascript_sets_object.htm
JavaScript - Sets
A JavaScript Set object is a collection of unique values. Each value can only occur once in a Set. A Set can hold any value of any data type. The sets are introduced to JavaScript in ECMAScript 6 (ES6).
DEV Community
dev.to › firebase007 › demystifying-the-set-object-3k9p
Understanding the new Set object in JavaScript - DEV Community
January 17, 2020 - On the other hand, if nothing is passed to the Set object or if its value is a primitive like null for example, its return value will be empty. Moving on, just like every object has a constructor function, where they derive their traits from including methods and properties, all instances of the Set object inherit from the Set prototype.
MakeUseOf
makeuseof.com › home › programming › 8 javascript set methods you should master today
8 JavaScript Set Methods You Should Master Today
October 30, 2021 - A JavaScript Set is a collection of values that are unique. Sets can hold values of any data type. It can be simple primitives, such as integers or strings, etc, or complex types like arrays or object literals.