Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  142] [ 2]  / answers: 1 / hits: 167946  / 11 Years ago, tue, june 25, 2013, 12:00:00

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.



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


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


Update:



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?


More From » performance

 Answers
30

The short version: Arrays are mostly faster than objects. But there is no 100% correct solution.



Update 2017 - Test and Results



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'});
}


test
test



Original Post - Explanation



There are some misconceptions in your question.



There are no associative arrays in Javascript. Only Arrays and Objects.



These are arrays:



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


This is an array, too:



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).



This is an object:



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


Here is a performance test of three possibilities:



Lookup Array vs Holey Array vs Object Performance Test



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


[#77428] Monday, June 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rianna

Total Points: 67
Total Questions: 113
Total Answers: 113

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;