What is the time complexity of javascript's array.indexOf?

前端 未结 3 1902
一生所求
一生所求 2020-12-09 01:51

Does indexOf simply walk through the array or does it do something that is faster? I know that this is implementation dependent but what does Chrome or Firefox do?

3条回答
  •  感情败类
    2020-12-09 02:01

    In ECMA6, you have the Set(), and then you can do:

    var obj = new Set();
    obj.add(1);
    obj.has(1) === true;
    obj.has(2) === false;
    

    The performance of has is O(1).

提交回复
热议问题