Open In App

Internal Working of Set in JavaScript

Last Updated : 08 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Set is a collection of unique values, i.e. values can not be repeated. The values can be primitive or objects. ES6 sets are ordered. Elements of the set can be iterated in the insertion order. In this article, we will check the internal working of a set in Javascript.

Working of Set

Sets provide access times that are sublinear on the number of elements in the collection. It means that sets are internally stored as a hashtable, so they have a time complexity of O(1) for searching. They are also sometimes stored as a search tree with a time complexity of O(log(N))

It uses value equality to determine uniqueness, comparing values based on their data type. Iteration is predictable since the values’ insertion sequence is retained. The Set data structure simplifies managing unique values in JavaScript applications.

In order to check the performance of a set we compare it with the includes() method of the array and generally it is found to be faster than the includes() method.

Example: Here is the basic example of a JavaScript set.

Javascript




let set1 = new Set([10, 20, 30, 30, 40, 40]);
  
for (const item of set1) {
    console.log(item);
}


Output: We can see the elements in the set are stored in the same order as they are defined and the value equality algorithm removes the duplicates before storing the data in the set.

Set(4) {10, 20, 30, 40}

Example 2: This example describes the uses of Set Object in JavaScript.

Javascript




const setVal = new Set();
  
for (let i = 0; i < 1000000; i++) {
    setVal.add(i);
}
  
const arr = Array.from(Array(1000000), (_, i) => i);
let include = arr.includes(999999)
  
console.time('set')
console.log(setVal.has(999999)); // Output: true
console.timeEnd('set')
  
console.time('Array')
console.log(arr.includes(999999)); // Output: true
console.timeEnd('Array')


Output: In this example, we compared has() method with the includes() method of the array. The set has() method takes less time than the includes() method of the array.

true
set: 0.126220703125 ms
true
Array: 0.48388671875 ms

Importance of Set:

  • Handling Collections: To handle collections of elements, such as eliminating duplicates from an array, use the set function. You may quickly get rid of duplicate elements by changing an array to a Set and back again.
  • Unique Values: A Set is mostly used to store distinctive values. By preventing duplicate values, it makes sure that each element in the set is distinct. For situations when you need to save a collection of unique values, Set is perfect.
  • Iteration: A Set is iterable, which means you can easily loop through its elements using for…of loops or by using methods such as forEach(). This makes it convenient to iterate over the unique values stored in a Set and perform operations on them.
  • Mathematical Set Operations: Set enables a variety of set operations in mathematics, including union, intersection, and difference. Using methods like add(), remove(), and forEach(), you can quickly carry out similar operations on Set objects.
  • Memory Efficiency:  Compared to other methods, such as manually looking for duplicates in an array or object, utilizing a Set can be more memory-efficient in situations when you have a big collection of data and want to assure their uniqueness.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads