JavaScript Set and WeakSet
When I started writing this post, I thought I could explain some of the ES6 new types. A post called "JS New Built-ins types in ES6" and an overkill. So I switch to a more specific topic, Map
, Set
, WeakMap
and WeakSet
. Then, I find out that there's a lot to talk about all those types.
Finally, I decide to simplify things in a series of two post. JavaScript Set and WeakSet and JavaScript Map and WeakMap.
Does that makes sense? Well, I hope that at least these two post are easy to read.
Set
A set is a pool of unique primitive values or object references.
var enemies = new Set();
// Append elements to the set.
enemies
.add('Daleks')
.add('Cybermen')
.add('Weeping Angels')
// Check if the set has a particular element.
enemies.has('The Silence') === false;
// Get the amount of elements in the set.
enemies.size === 3;
It's common that the set is a un-ordered list of elements. That's kind of true for JavaScript as well since you can't access an element by a index.
But iterate over a set and you'll see that the elements will appear in insertion order.
Diferences between Set
and Array
Array and Set are both iterators so you may ask, which one is better to use? Well there're some distinctions to talk about first.
There're no duplicated elements in Set
.
The has
method that Set
has may be the best way to check if an element is inside a group of elements.
Array
has a literal notation (var arr = [1, 2, 3];
). That's not the case for Set
where you have to invoke using the new
operator.
Under the hood, Set
objects are implemented using hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. 1
List of methods and properties
Name | Description |
---|---|
size |
Return the number of elements in the Set. |
add(value) |
Appends a new element. Returns the Set object. |
clear() |
Removes all elements. |
delete(value) |
Removes the element associated. |
entries() |
Returns a new Iterator with array of [value, value] for each element in the Set object. |
forEach(callback) |
Regular forEach . |
has(value) |
Returns true if the element is in the Set, false if it isn't. |
values() |
Returns a new Iterator object with the values of each element in the Set. |
keys() |
Is the same function as values() |
WeakSet
The WeakSet
is like Set
but be aware of the following differences:
The only type that you can store is Object
, no arbitrary values like Number
.
The elements are "held weakly", meaning that storing an object on a WeakSet won't prevent it from being garbage-collected. Elements will only appear in the collection if they're referenced in somewhere else.
The value of .length
will always be 0.
Store weakly held objects in a collection.
List of methods and properties
Name | Description |
---|---|
add(value) |
Appends a new object to the collection. |
delete(value) |
Removes the element associated. |
has(value) |
Returns true if the element is in the WeakSet, false if it isn't. |
Subscribe to RECodes
Get the latest posts delivered right to your inbox