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

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

问题描述

我有一个可能包含数千个对象的模型.我想知道存储它们并在获得单个对象后检索单个对象的最有效方法是什么.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.

所以这些是我正在考虑的 2 个选项.在选项一中,它是一个带有递增索引的简单数组.在选项 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.

非关联数组的选项一:

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];
    }
}

带有关联数组的选项二:

Option two with associative array:

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天全站免登陆