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

前端 未结 3 1901
一生所求
一生所求 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:21

    With no guarantees about the nature or order of items in an array, you cannot do better than O(n), so it would walk through the array. Even if it was to parallelise the actual search across CPUs (no idea if firefox or chrome do this, but they could), it does not change the time complexity with a finite number of CPUs.

提交回复
热议问题