Reviewing the efficiency of sets
Let's review the efficiency of each method by reviewing the Big O notation in terms of time of execution:
Method | MySet | Set | Explanation |
add(value) |
O(1) | O(1) | Constant time insertion into the object or underlying data structure. |
addAll(values) |
O(n) | O(n) | Calls add(value) for each value in the input array, where n is the size of the array. |
delete(value) |
O(1) | O(1) | Constant time deletion from the object or underlying data structure. |
has(value) |
O(1) | O(1) | Object lookup in both cases has constant time |
values() |
O(n) | O(n) | In MySet , it iterates over the object's keys. In Set , it creates an iterator that yields each value in linear time. |
size (getter) |
O(1) | O(1) | Returns the value of the #size property or equivalent in the native Set. |
isEmpty() |
O(1) | O(1) | Checks if #size is 0. |
values() |
O(n) | O(n) | In MySet , it iterates over the object's keys. In Set , it creates an iterator that yields each value in linear time. |
clear() |
O(1) | O(1) | Reassigns... |