Javascript Array lookup efficiency: associative vs. stored associative?

后端 未结 4 387
逝去的感伤
逝去的感伤 2021-02-04 19:52

I\'ve been reading, and they\'re saying that associative arrays won\'t give you the same efficiency as arrays. An associative array can look things up in O(N) time, where an ar

4条回答
  •  一向
    一向 (楼主)
    2021-02-04 20:25

    First of all, whoever they are, feel free to ignore them.

    Every decent implementation of every decent scripting language, including JavaScript, will give you associative arrays that are either O(log(n)) access time, or else O(1) average access time, O(n) worst case (which you almost never hit). Either way in practice a lookup is fast.

    Arrays have O(1) guaranteed access time, which is incredibly fast. But in some scripting languages (eg PHP) there isn't even a native array type provided. They just use associative arrays for both.

提交回复
热议问题