Array.from 的时间复杂度

Sho*_*ota 6 javascript time-complexity ecmascript-6

的时间复杂度是多少Array.from()。例如:

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
Run Code Online (Sandbox Code Playgroud)

Cer*_*nce 6

它是O(n)。当用于可迭代对象(如 a Set)时,Array.from迭代可迭代对象并将返回的每个项目放入新数组中,因此可迭代对象返回的每个项目都有一个操作。