Array.from的时间复杂度 [英] Time complexity of Array.from

查看:110
本文介绍了Array.from的时间复杂度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

它是 O(n)。当在可迭代对象(例如 Set )上使用时, Array.from 遍历该可迭代对象,并将返回的每个项目放入新数组,因此迭代器返回的每个项目都有一个操作。

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.

这篇关于Array.from的时间复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆