JavaScript中的数组与对象效率 [英] Array vs. Object efficiency in JavaScript

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

问题描述

我有一个可能有数千个物体的模型。我想知道什么是最有效的存储方式和一旦我拥有它的id后检索单个对象。 id是长号。

I have a model with possibly thousands of objects. I was wondering what would be the most efficient way of storing them and retrieving a single object once I have it's id. The id's are long numbers.

所以这些是我想到的两个选项。在选项1中,它是一个带有递增索引的简单数组。在选项2中,它是一个关联数组,也许是一个对象,如果它有所不同。我的问题是哪一个更有效率,当我主要需要检索单个对象时,有时还会循环遍历它们并进行排序。

So these are the 2 options I was thinking about. in option one it's a simple array with an incrementing index. in option 2 it's an associative array and maybe an object, if it makes a difference. My question is which one is more efficient, when I mostly need to retrieve a single object, but also sometimes loop through them and sort.

选项一,非关联数组:

Option one with non associative array:

var a = [{id: 29938, name: 'name1'},
         {id: 32994, name: 'name1'}];
function getObject(id) {
    for (var i=0; i < a.length; i++) {
        if (a[i].id == id) 
            return a[i];
    }
}

选项二带关联数组:

var a = [];  // maybe {} makes a difference?
a[29938] = {id: 29938, name: 'name1'};
a[32994] = {id: 32994, name: 'name1'};
function getObject(id) {
    return a[id];
}

更新:

好的,我知道在第二个选项中使用数组是不可能的。所以第二个选项的声明行应该是: var a = {}; ,唯一的问题是:在检索具有给定id的对象时表现更好:数组或id是键的对象。

OK, I get that using an array in the second option is out of the question. So the declaration line the second option should really be: var a = {}; and the only question is: what is performing better in retrieving an object with a given id: an array or an object where the id is the key.

并且,如果我必须多次对列表进行排序,答案是否会改变?

and also, will the answer change if i will have to sort the list many times?

推荐答案

简短版本:数组大多比对象快。但是没有100%正确的解决方案。

var a1 = [{id: 29938, name: 'name1'}, {id: 32994, name: 'name1'}];

var a2 = [];
a2[29938] = {id: 29938, name: 'name1'};
a2[32994] = {id: 32994, name: 'name1'};

var o = {};
o['29938'] = {id: 29938, name: 'name1'};
o['32994'] = {id: 32994, name: 'name1'};

for (var f = 0; f < 2000; f++) {
    var newNo = Math.floor(Math.random()*60000+10000);
    if (!o[newNo.toString()]) o[newNo.toString()] = {id: newNo, name: 'test'};
    if (!a2[newNo]) a2[newNo] = {id: newNo, name: 'test' };
    a1.push({id: newNo, name: 'test'});
}


您的问题存在一些误解。

There are some misconceptions in your question.

这些是数组:

var a1 = [1, 2, 3];
var a2 = ["a", "b", "c"];
var a3 = [];
a3[0] = "a";
a3[1] = "b";
a3[2] = "c";

这也是一个数组:

var a3 = [];
a3[29938] = "a";
a3[32994] = "b";

它基本上是一个带孔的数组,因为每个数组都有连续的索引。它比没有孔的阵列慢。但是手动遍历数组甚至更慢(大多数情况下)。

It's basically an array with holes in it, because every array does have continous indexing. It's slower than arrays without holes. But iterating manually through the array is even slower (mostly).

这是一个对象:

var a3 = {};
a3[29938] = "a";
a3[32994] = "b";

以下是三种可能性的性能测试:

Here is a performance test of three possibilities:

Smashing Magazine上关于这些主题的精彩读物:编写快速内存高效的JavaScript

An excellent read about these topics at Smashing Magazine: Writing fast memory efficient JavaScript

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

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