Javascript Sets and Weaksets

--

Hi!, I’m Gurkirpal Singh, currently enrolled in Google’s Reskilling India Scholarship Programme’s Mobile Web track. This article is my submission for “Beginner’s Guide to Mobile Web Development” activity task set 5.

In this article we will be talking about two new features added in ES6, that is, Sets and Weaksets and what are their features.

What is a Set?

In mathematics, a set is a collection of distinct objects, considered as an object in its own right. In the context of programming, a Set is a collection of items which are unique. No element of a set can be repeated. The elements can be of any type, even objects.

Sets were added as a feature in ES6. Following is the syntax to create a Set:

new Set([iterable]);

iterable can be any iterable object. If nothing is passed then the resulting Set will be an empty Set.

Now let’s see a simple example involving Sets.

Click here to explore on codepen.io

Here we added multiple elements with the same value but only one will be kept in the set. Next, we check if the specified elements exist in the set.

You might be wondering “But how does javascript make sure all elements are unique?”. According to docs, the algorithm used to check if two elements are same in a set is the same one used for the operator === . So, if a === b is true then only one of them will be added to the set.

Looping over sets

Looping sets is straightforward with for...of loop. Continuing with our previous example.

Click here to explore on codepen.io

This prints out all the members of the set. As you can see only one instance of same elements is kept.

Commonly used Set methods

Sets have some predefined methods that can be used for convenience. We have already seen Set.prototype.has(value) in action earlier.

Set.prototype.add(value)

Returns a set after adding the given to the set.

Click here to explore on codepen.io

Line #4 confirms that the set returned is the original set.

Set.prototype.clear()

As the name suggests, used to “clear” the Set. The resulting Set is an empty Set.

Click here to explore on codepen.io

Set.prototype.delete(value)

Attempts to remove value from the given Set. Returns true if value was present in the Set.

Click here to explore on codepen.io

Set.prototype.has(value)

Used to check if an element exists in the Set.

Click here to explore on codepen.io

Set.prototype.keys()

Returns an Iterator object which can be used to iterate over the values contained in the Set.

Click here to explore on codepen.io

Using .next() on the iterator returns an object with two properties:

  • value: The next element. undefined if no elements left.
  • done: A Boolean value indicating if the last element has been reached.

Now you have the basic knowledge about Sets and how to use them. After all is said and done, Sets aren’t a perfect solution for every problem involving sets…

Enter WeakSets

According to docs, The WeakSet object lets you store weakly held objects in a collection. Here “weakly held” means that, unlike in Sets, the garbage collector can remove objects from the WeakSet.

The syntax to create a WeakSet is new WeakSet([iterable]);

Another major difference is that a WeakSet only accepts objects as it’s members.

WeakSet methods

WeakSets don’t allow you to access the elements stored so they have fewer methods comparatively.

  • WeakSet.prototype.add(value) : Add a new member.
  • WeakSet.prototype.add(value) : Remove member from WeakSet.
  • WeakSet.prototype.has(value) : Checks if member exists in WeakSet.

These methods work similarly to their counterparts in Sets.

Why use WeakSets?

Here are some benefits of using WeakSets

  • WeakSet’s contents can be garbage collected.
  • Potential to reduce memory consumption.
  • Helpful in class branding

Let’s look at a popular example about WeakSets.

Here makeRequest() will throw an error if the invocation didn’t come from an instance of Request. Here Sets wouldn’t have done the job since their members aren’t garbage-collected.

And that’s it, Good job! Now you understand how to use Sets and Weaksets. Go ahead and use them in your next projects like a pro.

Resources

--

--