![]() |
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 SetSets 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
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
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:
|
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |