What would be the time complexity of Array.from(). For example:
const set = new Set();
set.add('car');
set.add('cat');
set.add('dog');
console.log(Array.from(set)); // time complexity of making this convertion from Set to Array
It's O(n). When used on an iterable (like a Set), Array.from iterates over the iterable and puts every item returned into the new array, so there's an operation for every item returned by the iterable.
It is always going to be O(n) as the number of iterations would be directly proportional to the number of elements in the set. Actual Time complexity would be O(n) for retrieving values from a set O(n) for pushing into an array.
O(n) + O(n) = O(2n)
But since we do not consider a constant value when calculating using n value it should be O(n).
Array.from()has a different "problem"; it's quite slow compared to other methods to create that Array. This seems to be due to the overhead generated by the wide variety of input types that the method may have to deal with. So for small data-structures this overhead may outweigh the execution time of the actual task at hand.