JavaScript数组的查找效率:联想与联想存储? [英] Javascript Array lookup efficiency: associative vs. stored associative?

查看:166
本文介绍了JavaScript数组的查找效率:联想与联想存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读,他们说,关联数组不会给你同样的效率阵列。关联数组可以看看东西在O(N)的时间,其中一个数组可以查找东西在O(1)。

下面是我的问题:哪一个会在快速查找值,且不占用太多内存方面更有效。

关联:

  VAR myVars =新的Array();
myVars ['测试1'] = A;
myVars ['TEST2'] = B;
myVars ['TEST3'] = C;
...(高达200+的值)回声myVars ['测试2'];

存储的关联:

  VAR myVars =新的Array();
变种TEST1 = 1;
变种TEST2 = 2;
VAR TEST3 = 3;
...(高达200+的值)myVars [TEST1] = A;
myVars [TEST2] = B;
myVars [TEST3] = C;
...(高达200+的值)回声myVars [TEST2]


解决方案

首先,阵列的第一种用法是错误的。虽然它的可能的做到这一点,这并不意味着你应该。你是滥用的事实,数组对象了。这可能会导致意外的行为,例如虽然你加200个值, myVars.length 0

不要使用JavaScript数组作为关联数组。使用普通的对象为:

  VAR myVars = {};
myVars ['测试1'] = A;
myVars ['TEST2'] = B;
myVars ['TEST3'] = C;

二,在JavaScript中有两个(对象和数组)之间没有真正的区别。阵列扩展对象,并添加一些行为,但他们仍然对象。元素被存储为阵列的属性

您可以在规范的更多信息:


  

Array对象给予特殊处理某一类的属性名称。属性名称P(以字符串值的形式)是一个数组的索引,当且仅当的ToString(ToUint32(P))等于P和ToUint32(P)不等于2 32 -1。 (...)


所以,两种:

  VAR的obj = {'答案':42};
OBJ ['答案'];

  VAR ARR = [42];
改编[0];

具有相同的访问时间,这绝对是的不可以 O(N)

†:这是更好地说的的有。显然,这在变化不同的实现。


除此之外,你的第二个例子是可怕的维护。如果您分配号码变数,为什么不直接使用数字?

  VAR myVars = [];
myVars [0] =一个;
myVars [1] = B;
myVars [2] = C;


更新:

更重要的是:你必须选择适合您需求的数据结构,这不仅是由一个单一的元素的访问时间确定的,同时也:


  • 的关键是连续号码或任意字符串/数字?

  • 你有没有访问所有(即对所有环路)的集合的元素?

数字阵列(阵列)和关联数组(或哈希表/图在JS(对象)),针对不同的问题提供了不同的解决方案。

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 array can look things up in O(1).

Here's my question: which one would be more efficient in terms of looking up values quickly and not hogging too much memory?

Associative:

var myVars=new Array(); 
myVars['test1'] = a;
myVars['test2'] = b;
myVars['test3'] = c;
... (up to 200+ values)

echo myVars['test2'];

Stored Associative:

var myVars=new Array(); 
var TEST1 = 1;
var TEST2 = 2;
var TEST3 = 3;
... (up to 200+ values)

myVars[TEST1] = a;
myVars[TEST2] = b;
myVars[TEST3] = c;
... (up to 200+ values)

echo myVars[TEST2];

解决方案

First, the first usage of Array is wrong. Although it is possible to do it, it does not mean you should. You are "abusing" the fact that arrays are objects too. This can lead to unexpected behaviour, e.g. although you add 200 values, myVars.length will be 0.

Don't use a JavaScript array as associative array. Use plain objects for that:

var myVars = {}; 
myVars['test1'] = a;
myVars['test2'] = b;
myVars['test3'] = c;

Second, in JavaScript there is no real difference between the two (objects and arrays). Arrays extend objects and add some behaviour, but they are still objects. The elements are stored as properties of the array.

You can find more information in the specification:

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1. (...)

So both:

var obj = {'answer': 42};
obj['answer'];

and

var arr = [42];
arr[0];

have the same access time, which is definitely not O(n).

†: It is better to say should have. Apparently this varies in different implementations.


Apart from that, your second example is horrible to maintain. If you assign numbers to variables, why not use the numbers directly?

var myVars = []; 
myVars[0] = a;
myVars[1] = b;
myVars[2] = c;


Update:

More importantly: You have to choose the right data structure for your needs and this is not only determined by the access time of a single element, but also:

  • Are the keys consecutive numbers or arbitrary strings/numbers?
  • Do you have to access all (i.e. loop over all) elements of the collection?

Numerical arrays (arrays) and associative arrays (or hash tables/maps (objects in JS)) provide different solutions for different problems.

这篇关于JavaScript数组的查找效率:联想与联想存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆