对象散列与对象数组首选项 [英] Object Hash vs. object Array preference

查看:54
本文介绍了对象散列与对象数组首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

在我的对象中,我有getDirectory()方法返回2维元素

数组

(或模仿2 -dimentional数组使用两个JavaScript对象

具有自动处理长度属性 - 请让'我们不要进入

每个点都在我的澄清讨论现在 - 但是你要打电话给你?
打电话 - 你打电话给它;-)

array [0]包含当前目录中所有文件的记录。 />
array [1]包含当前dir中所有subs的记录


每个记录又是一个包含6个元素的数组:file / folder

名称,大小,类型,属性,lastModified日期,lastModified时间。


考虑到有人需要建立对话框界面

关于这个数据IMHO数组结构是最合适的(因为
排序问题)。


或者每个目录条目上的自定义对象仍然会更多

fl灵活?为了更容易编程,你个人更喜欢从功能中获得什么?



解决方案

VK写道:

在我的对象中,我有getDirectory()方法返回2维数组
(或使用两个JavaScript对象模仿2维数组
具有自动处理长度属性 - 请让'我们不要进入一个
每个点我现在澄清讨论 - 但是你想打电话 - 你打电话给它; - )


我可以接受术语二维数组。为简洁起见,只要

每个元素具有相同数量的子元素。

array [0]包含当前目录中所有文件的记录。
array [ 1]包含当前dir中所有subs的记录

每个记录依次是6个元素的数组:文件/文件夹
名称,大小,类型,属性,lastModified日期, lastModified time。

考虑到有人需要建立一个对话框界面这个数据IMHO阵列结构是最合适的(导致排序问题的原因)。


我不明白这个说法。排序有什么用呢?

或者每个目录条目上的自定义对象仍然更灵活?为了更容易编程,你个人更喜欢离开功能?




我更喜欢这里的基于对象的对象,因为它更多很难

记住数组元素的索引比有意义的标识符

属性。如果稍后需要,可以将Array对象扩展为

a集合,以便可以使用这两种访问方法。

PointedEars


> > VK写道:

考虑到有人需要建立一个对话框接口这个数据IMHO阵列结构是最合适的(因为
排序问题。)


..托马斯''PointedEars''Lahn写道:
我不明白这个论点。什么排序与它有关?


我的意思是说你不能排序哈希键(对象属性,

集合项 - 军团就是它的名字),但你可以对数组进行排序。 br />
需要承认,如果像

记录[e1,e2,e3,e4,e5]>通过e4排序所有记录

我对最好接受的机制非常阴霾,因此我不知道什么是更好的服务:对象或数组。我曾经通过mySQL在服务器端处理这样的问题,其中无聊的部分在后台自动完成



.. Thomas ''PointedEars''Lahn写道:
我更喜欢这里基于对象的对象,因为要记住数组元素的索引比记住有意义的标识符更难。属性。如果稍后需要,可以将Array对象扩展为集合,以便可以使用这两种访问方法。



这不是一个可用性问题,而是一个问题

生产力。

首先,服务对象将用于LOOKUP操作

专属(你不能删除文件/属性或添加新的在这里)。

其次它将非常密集地用于LOOKUP操作。在一个

的大目录(超过100个文件)上显示/排序/选择将需要数千美元的查找次数,所以即使1ms的差异也会轻易加起来。我想

它提出了一个老问题:对象和数组之间是否有任何生产力差异

用于查找操作?


" VK" < SC ********** @ yahoo.com>写道:

我的意思是说你不能对哈希键(对象属性,
集合项 - 军团就是它的名字)进行排序,但你可以对数组进行排序。


排序意味着排序,这又意味着通过计算

数字可转换性,所以是的,数组元素可以排序,重新排列它们

属性名称。没有对象的一般属性的排序,

因此甚至不可能定义对它们进行排序的概念。

需要承认,以防万一像
记录[e1,e2,e3,e4,e5]>按e4排序所有记录


不明白。如果每个记录都是一个五元素数组,并且你希望

按它的第四个元素排序,那么我会做类似的事情:

---

//通用比较函数

函数cmp(a,b){

return(b< a) - (a< b);

}


//创建比较参数属性道具的函数。

函数cmpProperty(prop){

返回函数(a,b){

返回cmp(a [prop],b [prop]);

};

}


//按第四个元素排序记录。

records.sort(cmpProperty(3));

---

我对最好接受的机制非常多云,因此我不确定要提供什么更好的服务:对象或数组。


无所谓。你比较一个对象的属性,是否是'

被称为3。或foo或是无关紧要的。因此,如果您在任何地方使用该记录

else,请使用有意义的名称。


我想它会提出一个老问题:是否有任何生产力
用于查找操作的Object和Array之间的区别?




这取决于它运行的Javascript的实现。


A快速测试:

---

函数timeLookup(obj,prop,N){

var t0 = new Date();

var x;

while(N--){

x = obj [prop];

}

var t1 = new Date();

返回t1-t0;

}


var N = 1000000;

var a = [" lal,lal,lala,lalala,lalalaalla];

var t0 = timeLookup(a," 3",N);

var t1 = timeLookup(a,3,N);


var o = {" ;稀释剂:lal,daller:lal,y:lala,3:lal ala,

" anguish":lalalaalla};

var t2 = timeLookup(o," 3",N);


var o2 = {" diller":lal,daller:lal,y:lala,x:lal ala,

痛苦:" lalalaalla"};

var t3 = timeLookup(o2," x",N);


var o3 = {" diller":" lal"," daller":" lal"," y":" lala",

" dallerdallerdaller":" ; lalala,痛苦:lalalaall a};

var t4 = timeLookup(o3," dallerdallerdaller",N);


[t0,t1,t2,t3,t4]

---

给出以下结果(以ms为单位的时间):

数组[" 3"] array [3] object [" 3"] object [" x"] object [" daller ...."]

Opera 922 766 906 797 843

IE 6 906 891 891 937 1093

FF 746 406 750 625 906

所以,其中只有Firefox似乎有更多的

高效的数组查找比属性查找,但更长的属性

名称使查找时间更长。


它也应该在对象/数组上进行测试很多

的房产,而不仅仅是五个。


Opera和Firefox似乎需要额外的时间才能获得房产

字符串包含一个数字文字,即使在一个对象上使用,也不是
一个数组。人们可以想知道为什么:)


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''


Hello,
In my object I have getDirectory() method which returns 2-dimentional
array
(or an imitation of 2-dimentional array using two JavaScript objects
with auto-handled length property - please let''s us do not go into an
"each dot over i" clarification discussion now - however you want to
call - you call it ;-)

array[0] contains records of all files in the current dir.
array[1] contains records of all subs in the current dir

Each records is in its turn is an array of 6 elements: file/folder
name, size, type, attributes, lastModified date, lastModified time.

Tking into account that someone will need to build a dialog interface
over this data IMHO array structure is the most suitable (''cause of
sorting issue).

Or a custom object on each directory entry still would be more
flexible? What would you personally prefer to get out of the functiuon
for easier programming?

解决方案

VK wrote:

In my object I have getDirectory() method which returns 2-dimentional
array
(or an imitation of 2-dimentional array using two JavaScript objects
with auto-handled length property - please let''s us do not go into an
"each dot over i" clarification discussion now - however you want to
call - you call it ;-)
I can accept the term "2-dimensional array" for brevity, provided that
each element has the same number of subelements.
array[0] contains records of all files in the current dir.
array[1] contains records of all subs in the current dir

Each records is in its turn is an array of 6 elements: file/folder
name, size, type, attributes, lastModified date, lastModified time.

Tking into account that someone will need to build a dialog interface
over this data IMHO array structure is the most suitable (''cause of
sorting issue).
I don''t understand this argument. What has sorting to do with it?
Or a custom object on each directory entry still would be more
flexible? What would you personally prefer to get out of the functiuon
for easier programming?



I''d prefer an Object-based object here since it is more difficult to
remember the index of the array element than the meaningful identifier
of the property. If needed later, an Array object can be extended to
a collection so that both access methods can be used.
PointedEars


> > VK wrote:

Tking into account that someone will need to build a dialog interface
over this data IMHO array structure is the most suitable (''cause of
sorting issue).

.. Thomas ''PointedEars'' Lahn wrote:
I don''t understand this argument. What has sorting to do with it?
I meant to say that you cannot sort hash keys (object properties,
collection items - the Legion is its name) but you can sort arrays.
Need to admit though that in case like
record[e1, e2, e3, e4, e5] > sort all records by e4
I''m pretty cloudy about the best accepted mechanics and therefore I''m
not sure what to serve better: an Object or an Array. I used to handle
such issues on server-side through mySQL where the boring part is done
automatically on the background.
.. Thomas ''PointedEars'' Lahn wrote:
I''d prefer an Object-based object here since it is more difficult to
remember the index of the array element than the meaningful identifier
of the property. If needed later, an Array object can be extended to
a collection so that both access methods can be used.


That''s not so much a question of usability but a question of
productivity.
Firstly the served object will be used for LOOKUP operations
exclusively (you cannot delete files/properties or add new ones here).
Secondly it will be used for LOOKUP operations very intensively. On a
big directory (over 100 files) display/sorting/selection will take
thousands of lookups so even 1ms difference will add up easily. I guess
it brings up the old question: is there any productivity difference
between Object and Array for lookup operations?


"VK" <sc**********@yahoo.com> writes:

I meant to say that you cannot sort hash keys (object properties,
collection items - the Legion is its name) but you can sort arrays.
Sorting implies ordering, which again implies indexability by counting
numbers, so yes, array elements can be sorted, rearranging their
property names. There is no ordering of general properties of objects,
so it''s not even possible to define a notion of sorting on them.
Need to admit though that in case like
record[e1, e2, e3, e4, e5] > sort all records by e4
Not understood. If each record is a five element array, and you want
to sort it by its fourth element, then I''d do something like:
---
// generic comparison function
function cmp(a,b) {
return (b<a)-(a<b);
}

// creates function that compares property prop of arguments.
function cmpProperty(prop) {
return function (a,b) {
return cmp(a[prop],b[prop]);
};
}

// sort records by fourth element.
records.sort(cmpProperty(3));
---
I''m pretty cloudy about the best accepted mechanics and therefore I''m
not sure what to serve better: an Object or an Array.
Doesn''t matter. You compare a property of the object, whether it''s
called "3" or "foo" is irrelevant. So if you use the record anywhere
else, use meaningful names.

I guess it brings up the old question: is there any productivity
difference between Object and Array for lookup operations?



That depends on the implementation of Javascript it is running on.

A quick test:
---
function timeLookup(obj,prop, N) {
var t0 = new Date();
var x;
while(N--) {
x = obj[prop];
}
var t1 = new Date();
return t1-t0;
}

var N = 1000000;
var a = ["lal","lal","lala","lalala","lalalaalla"];
var t0 = timeLookup(a,"3", N);
var t1 = timeLookup(a,3, N);

var o = {"diller":"lal","daller":"lal","y":"lala","3":"lal ala",
"anguish":"lalalaalla"};
var t2 = timeLookup(o, "3", N);

var o2 = {"diller":"lal","daller":"lal","y":"lala","x":"lal ala",
"anguish":"lalalaalla"};
var t3 = timeLookup(o2, "x", N);

var o3 = {"diller":"lal","daller":"lal","y":"lala",
"dallerdallerdaller":"lalala","anguish":"lalalaall a"};
var t4 = timeLookup(o3, "dallerdallerdaller", N);

[t0,t1,t2,t3,t4]
---
gives the following results (times in ms):
array["3"] array[3] object["3"] object["x"] object["daller...."]
Opera 922 766 906 797 843
IE 6 906 891 891 937 1093
FF 746 406 750 625 906

So, of these, only Firefox seems to have a significantly more
efficient array lookup than property lookup, but longer property
names makes for longer lookup times.

It should probably also be tested on objects/arrays with lots
of properties, not just five.

Both Opera and Firefox seems to take extra time when the property is a
string containing a number literal, even when used on an object, not
an array. One can wonder why :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


这篇关于对象散列与对象数组首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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