Is the Set.has() method O(1) and Array.indexOf O(n)? [duplicate]
问题 This question already has answers here : Javascript ES6 computational/time complexity of collections (2 answers) Closed 9 months ago . I have seen in an answer that the Set.has() method is O(1) and Array.indexOf() is O(n). var a = [1, 2, 3, 4, 5]; a.indexOf(5); s = new Set(a); s.has(5); //Is this O(1)? Is Set.has() really O(1) ? 回答1: If one read the specification of has() , there is an algorithm describing it: Algorithm for Set.prototype.has(value) : The following steps are taken: Let S be