找到数组/对象项目最快方法数组、对象、最快、方法

2023-09-08 15:08:35 作者:人心似海深

什么是找到一个项目的在Javascript / AS3列表或对象的索引最快的方法?我问这两个因为语法是类似的这些语言。

What's the fastest way to find the index of an item in a list or object in Javascript / AS3? I'm asking this for both because the syntax is similar for both these languages.

假设:

myArray = ["one", "two", "three"];
myObject = {one:1, two:2, three:3};

这是最快,在这种情况下,你会想它呢?

Which is the fastest and in which case would you want it or not?

Array.indexOf(X) myObject的[X]!= NULL 的X myObject的 在别的东西?

推荐答案

使用对象不听起来像一个好主意,因为重新索引将是一个沉重的操作,将消灭接入性能提升(如有)。 Array.indexOf(X)似乎是要走的路。

Using an object doesn't sound like a good idea, as re-indexing will be a heavy operation and will wipe out the access performance gains if any. Array.indexOf(x) seems to be the way to go.