Number
, String
, Object
, Array
and Boolean
. In most cases, this is enough. But if you need to make the code as fast and scalable as possible, the use of these data types is not always justified.Set
data type, which allows you to work with collections of unique values, make the code faster. This is especially true for large-scale project code. The Array
and Set
types have a lot in common, but the use of the Set
data type is able to give the programmer such opportunities, which are clearly manifested during the execution of programs that the Array
type does not.Array
data type (we will call objects of this type “arrays”) is that arrays are indexed collections of values. This means that the data in the arrays is stored using indexes. const arr = [A, B, C, D]; console.log(arr.indexOf(A)); // : 0 console.log(arr.indexOf(C)); // : 2
Set
(we will call them "collections") are collections containing data in key / value format. Instead of using indexes, collections store items using keys. Elements stored in the collection can be iterated in the order they are added to the collection, and the collection cannot store the same elements. In other words, all elements of the collection must be unique.indexOf()
and includes()
, used to search for elements and check whether there is a certain element in the array, are slow.splice()
method, based on the index of the element. As with the search for items, deleting items using indexes is a slow operation.push()
and unshift()
to an array.NaN
value. The indexOf()
method cannot be used to search for NaN
values ​​in an array, while using the has()
collection method to find out if there is a NaN
in it.Set
store only unique values. If you need to avoid storing duplicate elements in a certain data structure, this is their significant advantage over arrays. When working with arrays to remove duplicate elements would have to write additional code.Set
type objects can be found here let arr = [], set = new Set(), n = 1000000; for (let i = 0; i < n; i++) { arr.push(i); set.add(i); }
123123
check the presence of the element 123123
in the array and in the collection, knowing in advance that there is such an element in these data structures. let result; console.time('Array'); result = arr.indexOf(123123) !== -1; console.timeEnd('Array'); console.time('Set'); result = set.has(123123); console.timeEnd('Set');
Array: 0.173ms Set: 0.023ms
console.time('Array'); arr.push(n); console.timeEnd('Array'); console.time('Set'); set.add(n); console.timeEnd('Set');
Array: 0.018ms Set: 0.003ms
const deleteFromArr = (arr, item) => { let index = arr.indexOf(item); return index !== -1 && arr.splice(index, 1); };
console.time('Array'); deleteFromArr(arr, n); console.timeEnd('Array'); console.time('Set'); set.delete(n); console.timeEnd('Set');
Array: 1.122ms Set: 0.015ms
const duplicateCollection = ['A', 'B', 'B', 'C', 'D', 'B', 'C']; // let uniqueCollection = new Set(duplicateCollection); console.log(uniqueCollection) // : Set(4) {"A", "B", "C", "D"} // let uniqueCollection = [...new Set(duplicateCollection)]; console.log(uniqueCollection) // : ["A", "B", "C", "D"]
Set
data structure would necessarily be used to solve the problem.sum
. Write a function that returns true
if, as a result of the addition of any two elements of this array, we get the value of sum
. If there are no such elements in the array, the function should return false
.[3, 5, 1, 4]
and the value of sum
is equal to 9
, then the function should return true
, since 4+5=9
.Set
data structure, to which values ​​will be added that complement the found values ​​to the sum
value.3
, we can add the number 6
to the collection, since we know that we need to find two numbers that are 9
in total. Then, every time we encounter a new value from the array, we can check the collection and see if it is there. When we meet the number 5
, we will add 4
to the collection. And when, finally, we get to the number 4
, we find it in the collection and we can return true
. const findSum = (arr, val) => { let searchValues = new Set(); searchValues.add(val - arr[0]); for (let i = 1, length = arr.length; i < length; i++) { let searchVal = val - arr[i]; if (searchValues.has(arr[i])) { return true; } else { searchValues.add(searchVal); } }; return false; };
const findSum = (arr, sum) => arr.some((set => n => set.has(n) || !set.add(sum - n))(new Set));
Set.prototype.has()
method is O (1), using the Set
data structure to store numbers that complement the numbers found in the array to a given value allows you to find a solution in linear time (O (N)).Array.prototype.indexOf()
method or on the Array.prototype.includes()
method, the time complexity of each of which is O (N), then the total time complexity of the algorithm would be O (N 2 ). As a result, it would become much slower.Set
data type in JavaScript, then we hope that you, having got an idea about it, will be able to use it in your projects with use.Set
data structure in your code?Source: https://habr.com/ru/post/447578/
All Articles