Last Updated : 23 Jul, 2025
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 according to the insertion order. In this article, we will check the internal working of a set in Javascript.
Working of SetSets in JavaScript internally use Hash Table which means the operations like search, insert and delete work in constant or O(1) time on average and all the stored items are unique.
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 s = new Set([10, 20, 30, 30, 40, 40]);
for (const item of s) {
console.log(item);
}
Set(4) {10, 20, 30, 40}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.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.
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4